I\'m using the datatables plugin with server-side data and am updating the table using AJAX.
My dataTables setup looks like this:
tblOrders = parame
All the solutions mentioned before have some problem (for example, the additional user http paramaters are not posted or are stale). So I came up with following solution that works well.
Extension function (My params are array of key value pairs)
<pre>
$.fn.dataTableExt.oApi.fnReloadAjax = function (oSettings, sNewSource, myParams ) {
if ( oSettings.oFeatures.bServerSide ) {
oSettings.aoServerParams = [];
oSettings.aoServerParams.push({"sName": "user",
"fn": function (aoData) {
for (var i=0;i<myParams.length;i++){
aoData.push( {"name" : myParams[i][0], "value" : myParams[i][1]});
}
}});
this.fnClearTable(oSettings);
this.fnDraw();
return;
}
};
</pre>
Example usage to put in you refresh event listener.
<pre>
oTable.fnReloadAjax(oTable.oSettings, supplier, val);
</pre>
Just one thing to pay attention to. Do not redraw table, once it's created, beacuse it's time consuming. Therefore, be sure to draw it only the first time. Otherwise, reload it
<pre>
var oTable;
if (oTable == null) {
oTable = $(".items").dataTable(/* your inti stuff here */); {
}else{
oTable.fnReloadAjax(oTable.oSettings, supplier, val);
}
</pre>
Similar to Mitja Gustin answer. Changed a loop, added sNewSource.
$.fn.dataTableExt.oApi.fnReloadAjax = function (oSettings, sNewSource, myParams ) {
if(oSettings.oFeatures.bServerSide) {
if ( typeof sNewSource != 'undefined' && sNewSource != null ) {
oSettings.sAjaxSource = sNewSource;
}
oSettings.aoServerParams = [];
oSettings.aoServerParams.push({"sName": "user",
"fn": function (aoData) {
for(var index in myParams) {
aoData.push( { "name" : index, "value" : myParams[index] });
}
}
});
this.fnClearTable(oSettings);
return;
}
};
var myArray = {
"key1": "value1",
"key2": "value2"
};
var oTable = $("#myTable").dataTable();
oTable.fnReloadAjax(oTable.oSettings, myArray);
I know this is late to the game, but fnDraw (from this answer above - which should be the accepted answer), is deprecated as of v1.10
The new method is:
this.api( true ).draw( true );
Which, BTW, has a comment that reads:
// Note that this isn't an exact match to the old call to _fnDraw - it takes
// into account the new data, but can hold position.
I found this script some time ago (so I don't remember where it came from :( and who to credit for it :'( ) but here :
$.fn.dataTableExt.oApi.fnReloadAjax = function (oSettings, sNewSource, fnCallback, bStandingRedraw) {
if (typeof sNewSource != 'undefined' && sNewSource != null) {
oSettings.sAjaxSource = sNewSource;
}
this.oApi._fnProcessingDisplay(oSettings, true);
var that = this;
var iStart = oSettings._iDisplayStart;
var aData = [];
this.oApi._fnServerParams(oSettings, aData);
oSettings.fnServerData(oSettings.sAjaxSource, aData, function (json) {
/* Clear the old information from the table */
that.oApi._fnClearTable(oSettings);
/* Got the data - add it to the table */
var aData = (oSettings.sAjaxDataProp !== "") ?
that.oApi._fnGetObjectDataFn(oSettings.sAjaxDataProp)(json) : json;
for (var i = 0; i < aData.length; i++) {
that.oApi._fnAddData(oSettings, aData[i]);
}
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
that.fnDraw();
if (typeof bStandingRedraw != 'undefined' && bStandingRedraw === true) {
oSettings._iDisplayStart = iStart;
that.fnDraw(false);
}
that.oApi._fnProcessingDisplay(oSettings, false);
/* Callback user function - for event handlers etc */
if (typeof fnCallback == 'function' && fnCallback != null) {
fnCallback(oSettings);
}
}, oSettings);
}
Add that BEFORE you call the datatable initialization function. then you can just call the reload like this:
$("#userFilter").on("change", function () {
oTable.fnReloadAjax(); // In your case this would be 'tblOrders.fnReloadAjax();'
});
userFilter is an ID for a dropdown, so when it changes, it reloads the data for the table. I just added this as an example but you can trigger it on any event.
From a discussion here, Allan (the DataTables guy) suggests that simply calling fnDraw will yield the results you're looking for. This is the method I use for reloading server-side stuff (via fnServerData, which is important), and it's worked so far.
$("#userFilter").on("change", function() {
oTable.fnDraw(); // In your case this would be 'tblOrders.fnDraw();'
});
For reload of the data, simply you need to select the DataTable using jquery selector with DataTable() function and call _fnAjaxUpdate function.
Here is example:
$('#exampleDataTable').DataTable()._fnAjaxUpdate();