Ajax load, but table don't draw the datas
Ajax load, but table don't draw the datas
poisons
Posts: 25Questions: 3Answers: 0
I've a simple datatable, I initialise like this:
var table = $('#products-details').DataTable({
ajax: {
url: "url.php",
contentType: "application/json",
"columns": [
{ "data": "name" } ,
{ "data": "options" },
{ "data": "quantity" } ,
{ "data": "action" }
],
data: function ( d ) {
d.locationId = $('#fromTransfer').val();
}
},
dom: '<"row"<"col-sm-6"l><"col-sm-6"f>><"table-responsive"t>p',
order: [[0, 'asc'], [1, 'asc']]
});
Then on a select change event I call an ajax reload
$("#fromTransfer").on('change', function () {
table.ajax.reload();
});
From the ajax file I've a json response (filled with dummy values)
{"data":{"name":"1","options":"1","quantity":"2","action":"3"}}
but the table don't redraw with new datas.
I already tried adding processing: true
and serverSide: true
to the datatable initialisation, and also draw
, recordsTotal
and recordsFiltered
to the JSon object but nothing changes
This discussion has been closed.
Replies
You need to return an array of objects. Instead of this:
You need this:
This is described in this section of the Data doc.
Kevin
Hi Kevin,
thanks for your answer, but the result is anyway empty table.
I've modified the php and now the ajax return this
{"data":[{"name":"1","options":"1","quantity":"2","action":"3"}]}
but the table remain empty
Giovanni
I have slightly modified the js, adding dataSrc and deleting some quote:
Now instead of an empty table I've a popup error, saying the table has Requested unknown parameter '0' for row 0, column 0
Reading the docs it means that the table is expecting something and getting something different.
I cannot see where is the error.
This is what url.php return
and this is the html
I see now.
columns definition has to be out from ajax call.
Thanks Kevin for helping
This is the right js: