简体   繁体   English

ajax后重新加载数据表

[英]Reload datatable after ajax

I use JQuery dataTable version 1.9.我使用 JQuery 数据表 1.9 版。 I need to reload a datatable after ajax success because I filter the elements after my query so the pagination shows all elements and not just the "real" ones filtered after the query.我需要在 ajax 成功后重新加载数据表,因为我在查询后过滤元素,因此分页显示所有元素,而不仅仅是查询后过滤的“真实”元素。 I know it is wrong to do this but I can only check after the query我知道这样做是错误的,但我只能在查询后检查

Here it seems that there are no elements这里好像没有元素

在此处输入图片说明

But when scrolling through the pagination the elements are there但是当滚动分页时,元素就在那里

在此处输入图片说明

you can use setInterval for reloading the datatable after ajax reuqest您可以使用 setInterval 在 ajax reuqest 后重新加载数据表

setInterval(function(){
    $('#tableid').DataTable({
 });
}, 300);

Please test this solution请测试此解决方案

function refreshTable() {
  $('.dataTable').each(function() {
      dt = $(this).dataTable();
      dt.fnDraw();
  })
}

If you are going to reload datatable after ajax call, then, you should have the data as a list of object.如果您要在 ajax 调用后重新加载数据表,那么您应该将数据作为对象列表。 Let's say it is stored in data Then, reload table by假设它存储在data然后,通过重新加载表

function reloadDataTable(tableObject, data) {
    tableObject.clear().rows.add(data).draw();
};

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

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