简体   繁体   English

jQuery DataTable 分页刷新

[英]jQuery DataTable pagination refresh

I am using DataTable for pagination in my table.我在我的表格中使用DataTable进行分页。 The content in the table is loading by making ajax request.表中的内容是通过 ajax 请求加载的。 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).问题是我正在使用我自己的函数来填充数据表(换句话说,我手动操作表的 DOM)。 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.我试过阅读API,但我发现的只是ajax API,它不适合我的代码,因为我想自己操作数据。
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.正如我发现 DataTable 在调用第一个DataTable()并在内部保存它时解析表。 So I have to clear that data before my ajax request:所以我必须在我的 ajax 请求之前清除这些数据:

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 1° - 创建一个函数来删除你的数据表

 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 2° - 创建一个函数来创建一个新的 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 3° - 在你的代码中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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