[英]jQuery DataTable pagination refresh
我在我的表格中使用DataTable进行分页。 表中的内容是通过 ajax 请求加载的。 问题是我正在使用我自己的函数来填充数据表(换句话说,我手动操作表的 DOM)。 那么有没有办法重新加载分页? 我试过阅读API,但我发现的只是ajax
API,它不适合我的代码,因为我想自己操作数据。
提前致谢,希望问题很清楚。
我找到了答案。 正如我发现 DataTable 在调用第一个DataTable()
并在内部保存它时解析表。 所以我必须在我的 ajax 请求之前清除这些数据:
var table = $('#mytable').DataTable();
table.clear();
然后我添加数据并循环绘制:
for(var k in data) {
// do smth with data
table.row.add([
k.field1,
k.field2
// ...
]).draw();
}
1° - 创建一个函数来删除你的数据表
function destroyDataTable(tableId){
if ( $.fn.dataTable.isDataTable( tableId ) == true){ // verify if DataTable exists
$(tableId).DataTable().destroy();
}
}
2° - 创建一个函数来创建一个新的 DataTable
function createDataTable(tableId){
// create only if DataTableDoes not exists
if ( $.fn.dataTable.isDataTable( tableId ) == false) {
$(tableId).DataTable( { /* your DataTable configuration */ } );
}}
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.