简体   繁体   中英

jQuery DataTable pagination refresh

I am using DataTable for pagination in my table. The content in the table is loading by making ajax request. The problem is that I'm using my own functions that fill table with data(in other words, I operate with table's DOM manually). So is there a way to reload just pagination? I have tried reading the API, but all I found is ajax API, and it doesn't fit my code since I want to operate with data by myself.
Thanks in advance, hope the question is clear.

I found an answer. As I found out the DataTable parse the table when first DataTable() is invoked and holds it internally. So I have to clear that data before my ajax request:

var table = $('#mytable').DataTable();
table.clear();

Then I add data and draw it in a cycle:

for(var k in data) {
    // do smth with data
    table.row.add([
        k.field1,
        k.field2
        // ...
    ]).draw();
}

1° - create a function to delete your DataTable

 function destroyDataTable(tableId){

  if ( $.fn.dataTable.isDataTable( tableId ) == true){ // verify if DataTable exists

        $(tableId).DataTable().destroy();

        }
}

2° - create a function to create a new DataTable

 function createDataTable(tableId){

   // create only if DataTableDoes not exists
  if ( $.fn.dataTable.isDataTable( tableId ) == false) { 

     $(tableId).DataTable( { /* your DataTable configuration */ } ); 
}}

3° - In your code

destroyDataTable('#myTable'); //Destroy old dataTable with the old data

/* call your function to populate your table with the new data */

createDataTable('#myTable'); // recreate the DataTable for the new data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM