简体   繁体   English

如何刷新DataTables排序和筛选信息

[英]How to refresh DataTables sort and filter info

For simplicity of the example I have a DataTable which I load from HTML. 为了简化示例,我有一个从HTML加载的DataTable。

This DataTable is having parts of its content updated through jQuery BUT the updated content while visible in the table, does not reflect when sorting or filtering. 该DataTable的部分内容通过jQuery进行了更新,但更新后的内容在表中可见,但在排序或过滤时不会反映出来。

See html code below 见下面的HTML代码

<table id="example" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Votes</th>
      <th>Location</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>John</th>
      <th>Doe</th>
      <th id="entry1_votes">50</th>
      <th>London</th>
    </tr>
    <tr>
      <th>Hill</th>
      <th>Vaught</th>
      <th id="entry2_votes">120</th>
      <th>Berlin</th>
    </tr>
    <tr>
      <th>Charles</th>
      <th>Roy</th>
      <th id="entry3_votes">78</th>
      <th>Liege</th>
    </tr>
  </tbody>
</table>

and javascript 和JavaScript

$(document).ready(function() {
  var dt = $('#example').DataTable({});

  $("#entry2_votes").text(60);
});

So if you try sorting Votes column or try filtering by the new value 60 set through jQuery it wont work 因此,如果您尝试对“ 投票”列进行排序或尝试通过jQuery设置的新值60进行过滤,那么它将无法正常工作

See this working example https://jsfiddle.net/bpali/d97bpqvs/3/ 参见此工作示例https://jsfiddle.net/bpali/d97bpqvs/3/

Obviously, my question is how to make it work as in my real life situation I have to update parts of the DataTable constantly through different Ajax requests of different page sections and I cannot just put an ajax source on the table and reload the table. 显然,我的问题是如何使其发挥作用,因为在现实生活中,我必须通过不同页面部分的不同Ajax请求不断更新DataTable的各个部分,而我不能仅将ajax源放在表上并重新加载表。

This is the correct way: 这是正确的方法:

 $(document).ready(function() { var dt = $('#example').DataTable({}); dt.cell( $("#entry2_votes") ).data(60) ; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" rel="stylesheet"/> <script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script> <table id="example" class="display" width="100%" cellspacing="0"> <thead> <tr> <th>First name</th> <th>Last name</th> <th>Votes</th> <th>Location</th> </tr> </thead> <tbody> <tr> <th>John</th> <th>Doe</th> <th id="entry1_votes">50</th> <th>London</th> </tr> <tr> <th>Hill</th> <th>Vaught</th> <th id="entry2_votes">120</th> <th>Berlin</th> </tr> <tr> <th>Charles</th> <th>Roy</th> <th id="entry3_votes">78</th> <th>Liege</th> </tr> </tbody> </table> 

Fiddle: https://jsfiddle.net/HappyiPhone/d97bpqvs/7/ 小提琴: https : //jsfiddle.net/HappyiPhone/d97bpqvs/7/

Check online demo 查看在线演示

You have to reinitialize data table after updating with jquery or you can update by datatable in-built methods/api 使用jquery更新后,您必须重新初始化数据表,或者可以通过datatable内置方法/ api更新

  $('#example').DataTable().destroy();
  $('#example').DataTable().draw();

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

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