简体   繁体   English

使用jQuery更新表格后,DataTables fnFilter无法正常工作

[英]DataTables fnFilter not working after updating table with jQuery

I use DataTables jQuery library, for a table in my project. 我将DataTables jQuery库用于项目中的表。

To customise filtering I've added span tags with filter words to my first column in each row. 为了自定义过滤,我在每行的第一列中添加了带有过滤词的span标签。 Example: 例:

<span style="display:none;" id="spanFilter' + i + '">filterWord</span>

Then I use select to filter on these words. 然后,我使用select过滤这些单词。

$("#filterSelect").on('change', function () {
    oTable.fnFilter($("#filterSelect option:selected").val(), 0);
});

This works fine. 这很好。

I also have code to change the filterWord of a #spanFilter . 我也有代码来更改#spanFilter的#spanFilter

$("#spanFilter" + i).text(spanFilterValue);

After changing the filterWord with jQuery the fnFilter does not work properly. 用jQuery更改filterWord后, fnFilter无法正常工作。

What am I doing wrong? 我究竟做错了什么?

By looking at your code I believe you are using dataTables legacy version . 通过查看代码,我相信您正在使用dataTables旧版本 If you just started using dataTables I'll strongly suggest you use the latest version. 如果您刚开始使用dataTables,我强烈建议您使用最新版本。

Anyway, to reflect back in dataTables I'll strongly suggest you to use dataTables api to change the content of the table instead of using jQuery. 无论如何,为了反映在dataTables中,我强烈建议您使用dataTables api更改表的内容,而不是使用jQuery。

Note: Following example is for DataTables v1.9 注意:以下示例适用于DataTables v1.9

$(document).ready( function () {

  var oTable = $('#table').dataTable();

  // get an array of the TR nodes that are used in table's body
  var nNodes = oTable.fnGetNodes();

  // iterating over each TR or row
  nNodes.forEach(function(node,index) {

    // update first column of every node
    oTable.fnUpdate(spanFilterValue,node,0);

  });

});

APIs References API参考

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

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