I have a datatable inside a form with the data generated using a JQuery DataTable with one of the columns have a select list control
JS:
$(".detailTbl").DataTable({
"ajax": {
"url": loadDetailHeader + "?code=" + code + "&ver=" + ver,
"type": "POST",
"dataSrc": ""
},
"columns": [
{
"data": null,
"render": function (data, type, full, meta) {
return meta.row + 1;
}
},
{
"data": "approverPositionId",
"render": function (data) {
return createListPosition(data,buttonCount)
}
},
{
"data": null,
"defaultContent": "<i class='fa fas fa-trash-alt' onclick='deleteNewRow(this)'></i>"
}
],
"serverSide": false, // for process server side
"filter": false, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once,
"info": false,
"paging": false
})
I also added a function to add new row to the datatable:
var t = $(".detailTbl").DataTable()
t.row.add([
null,
null,
null
]).draw();
buttonCount = buttonCount+1
everytime a new row button is clicked it run the add new row function. after finish adding the neccessary data, a submit button is clicked to save all the data in the datatable.
After reading the docs and some posts out there i managed to get the data but the result isn’t what i was expecting.
function saveDetail() {
var t = $(".detailTbl").DataTable()
var data = t.rows().data().toArray()
var data2 = t.$('select').serialize()
console.log(data)
console.log(data2)
}
Result:
as you can see the serialize method worked fine but i have no idea how to convert it to a json like this:
[
{ "posId": detail-select-1.value},
{ "posId": detail-select-2.value},
{ "posId": detail-select-3.value}
.... and so on
]
Is there a way to do it? Other solution is also welcomed.
Sorry for making this too long