Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redraw datatables after using ajax to refresh the table content?

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);     }; }, 
like image 525
Anagio Avatar asked Oct 19 '11 09:10

Anagio


People also ask

How do you refresh a page after Ajax success?

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.

What is Deferrender in DataTables?

This option allows DataTables to create the nodes (rows and cells in the table body) only when they are needed for a draw.


1 Answers

It looks as if you could use the API functions to

  • clear the table ( fnClearTable )
  • add new data to the table ( fnAddData)
  • redraw the table ( fnDraw )

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         ]      }      ); } 
like image 59
swatkins Avatar answered Sep 28 '22 16:09

swatkins