Wrap the datatable initialization into a function, like initDataTable()
, and add the option bDestroy
to the settings :
function initDataTable() {
if (fnServerObjectToArray) {
var oTable = $('.datatable').dataTable({
"bDestroy" : true, //<-- add this option
"bJQueryUI" : true,
...
//anything as above
});
}
}
$(document).ready(function () {
initDataTable();
});
when you want to refresh / reload, like by a click on a button :
<button id="refresh">Refresh</button>
$("#refresh").click(function() {
initDataTable();
});
here is a demo -> http://jsfiddle.net/cxe5L/
To avoid the cache-issue
jQuery dataTables has hardcoded cache : true
into its AJAX-calls. You have "sAjaxSource": $('.datatable').attr('data')
. I assume data
holds the path to an external resource, like /data/getdata.asp
or similar? Add a timestamp as param to that resource, like
sAjaxSource : $('.datatable').attr('data')+'?param='+new Date().getTime()
so the sAjaxSource becomes on the form
/data/getdata.asp?param=1401278688565
This is basically the same thing jQuery is doing, when cache : false
is set on a AJAX-request.
Source: https://stackoverflow.com/questions/23909590/how-to-refresh-datatable-list-jquery