Using a datatables 1.10 with dom created table. I am attempting to add a new row of data within an ajax response yet nothing happens. The same code by itself (no ajax) works perfectly fine. Yes, the response.success is returning 'true'.
// this works and adds the new row to the table
$('#test').on('click', function () {
dt.row.add( [
'td0',
'td1',
'td2',
'td3'
] ).draw();
});
//same code does not work within an ajax response...
$('#dtCreate').on('click', function () {
$.ajax({
type: 'post',
url: '/test/process/p_db_create.php'
}).done(function (response) {
//double check response
console.log(response);
if (response.success)
{
//add the row since this is not serverside
dt.row.add( [
'td0',
'td1',
'td2',
'td3'
] ).draw();
...more code below...
row.add is not appropriate for server-side processing. You are trying to mix data types in your table. Your AJAX is bring back and array of arrays (rows and cells). Your first add is all adding an array of cells so it works. Your second add is an object with named fields so DataTable does not know what to do with them.
This is needed because DataTables expects the data array to be called data - and the normal approach (using dataSrc: 'row_objects') is not available when the ajax function is used. An example of the JSON handled by the above code is:
As you state here, adding a row dynamically to a responsive DataTable works the same way as adding it to a non-responsive DataTable; using row.add (). For some reason, this is not working as shown here. Am I doing something wrong or is it broken?
With server side processing you will need to use an ajax request to add the row to your data source. The row will be displayed when appropriate through normal server side processing events. This discussion has been closed.
the td should be initiated by DataTable() not dataTable(). I ran into this problem and wasted my good few hours.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With