I am using Datatables and have a button on the page that refreshes the table using AJAX. To be clear the table isn't using an ajax source of data, we are just using ajax to refresh it only when needed. Ajax is refreshing the div which the table is wrapped in. I know i'm losing my pagination buttons and filtering capability because the table needs to be redrawn but i'm not sure how to add this into the table initialization code.
Datatables code
var oTable6; $(document).ready(function() { oTable6 = $('#rankings').dataTable( { "sDom":'t<"bottom"filp><"clear">', "bAutoWidth": false, "sPaginationType": "full_numbers", "aoColumns": [ { "bSortable": false, "sWidth": "10px" }, null, null, null, null, null, null, null, null, null, null, null ] }); });
The ajax code is this
$("#ajaxchange").click(function(){ var campaign_id = $("#campaigns_id").val(); var fromDate = $("#from").val(); var toDate = $("#to").val(); var url = 'http://domain.com/account/campaign/ajaxrefreshgrid?format=html'; $.post(url, { campaignId: campaign_id, fromdate: fromDate, todate: toDate}, function( data ) { $("#ajaxresponse").html(data); }); });
I tried this but it didn't work
"fnDrawCallback": function() { function( data ) { $("#ajaxresponse").html(data); }; },
reload() method, will reload (or refresh) an entire web page after the Ajax has performed its operation, that is, extracted data from an xml file. Therefore, I have added location. reload() inside the success callback function.
This option allows DataTables to create the nodes (rows and cells in the table body) only when they are needed for a draw.
It looks as if you could use the API functions to
http://datatables.net/api
UPDATE
I guess you're using the DOM Data Source (for server-side processing) to generate your table. I didn't really get that at first, so my previous answer won't work for that.
To get it to work without rewriting your server side code:
What you'll need to do is totally remove the old table (in the dom) and replace it with the ajax result content, then reinitialize the datatable:
// in your $.post callback: function (data) { // remove the old table $("#ajaxresponse").children().remove(); // replace with the new table $("#ajaxresponse").html(data); // reinitialize the datatable $('#rankings').dataTable( { "sDom":'t<"bottom"filp><"clear">', "bAutoWidth": false, "sPaginationType": "full_numbers", "aoColumns": [ { "bSortable": false, "sWidth": "10px" }, null, null, null, null, null, null, null, null, null, null, null ] } ); }
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