I am trying to implement functionality whereby clicking a button on the screen will cause my jQuery dataTable to refresh (as the server-side data source may have changed since the dataTable was created).
Here's what I have:
$(document).ready(function() {
$("#my-button").click(function() {
$("#my-datatable").dataTable().fnReloadAjax();
});
});
But when I run this, it does nothing. What's the proper way to refresh the dataTable when the button is clicked? Thanks in advance!
If you want to add a reload/refresh button to DataTables 1.10 then use drawCallback.
See example below (I am using DataTables with bootstrap css)
if using datatable v1.10.12 then simply calling
.draw()
method and passing your required draw types iefull-reset
,page
then you will re-draw your dt with new datalet dt = $("#my-datatable").datatable()
// do some action
dt.draw('full-reset')
for more check out datatable docs
.......}
Try destroy datatable first then setup it again, example
I had the same problem, this is how i fixed it:
first get the data with method of your choice, i use ajax after submitting results that will make change to the table. Then clear and add fresh data:
here is the source: https://datatables.net/reference/api/clear()
well, you didn't show how/where you are loading the scripts, but to use the plug-in API functions, you have to include it in your page after you load the DataTables library but before you initialize the DataTable.
like this