I have checked several questions already about this topic here in stackoverflow, but they are all using the old dataTable. I am using DataTable. I populated my DataTable by
datatable.rows().iterator('row', function ( context, index ) {
var data = this.row(index).data();
var row = $(this.row(index).node());
data[0] = 'new data';
datatable.row(row).data(data).draw();
});
Another alternative is
dtColumns[index].visible = false/true;
To show or hide any column.
If you want to refresh the table without adding new data then use this:
First, create the API variable of your table like this:
var myTableApi = $('#mytable').DataTable(); // D must be Capital in this.
And then use refresh code wherever you want:
myTableApi.search(jQuery('input[type="search"]').val()).draw() ;
It will search data table with current search value (even if it's blank) and refresh data,, this work even if Datatable has server-side processing enabled.
You have to first clear the table and then add new data using row.add() function. At last step adjust also column size so that table renders correctly.
$('#upload-new-data').on('click', function () {
datatable.clear().draw();
datatable.rows.add(NewlyCreatedData); // Add new data
datatable.columns.adjust().draw(); // Redraw the DataTable
});
Also if you want to find a mapping between old and new datatable API functions bookmark this
The following worked really well for me. I needed to redraw the datatable with a different subset of the data based on a parameter.
table.ajax.url('NewDataUrl?parameter=' + param).load();
If your data is static, then use this:
table.ajax.url('NewDataUrl').load();
The accepted answer calls the draw
function twice. I can't see why that would be needed. In fact, if your new data has the same columns as the old data, you can accomplish this in one line:
datatable.clear().rows.add(newData).draw();