简体   繁体   English

jQuery Datatables 自定义过滤不适用于所有表

[英]jQuery Datatables custom filtering not working on all tables

I am using the jquery plug-in Datatables 1.10 and I want to use the custom search to filter two tables on the same page, like this:我正在使用 jquery 插件 Datatables 1.10,我想使用自定义搜索来过滤同一页面上的两个表,如下所示:

function filterTableByErrorClass(propertiesTable, errorClassName) {
    $.fn.dataTable.ext.search.pop();
    $.fn.dataTable.ext.search.push(
    function(settings, data, dataIndex) {
        return $(propertiesTable.row(dataIndex).node()).find('td > div').hasClass(errorClassName);
    });
    propertiesTable.draw();
}

My problem is that this only works for one of the table, the one that is ready when the page loads.我的问题是,这只适用于其中一个表,即页面加载时就绪的表。 The other one is inside a js modal and is loaded using an ajax call (see below).另一个在 js 模式中,并使用 ajax 调用加载(见下文)。 It is loaded as html and after it is loaded, it should behave the same as the other table, eg all filtering should be done client-side.它作为 html 加载,加载后,它的行为应与其他表相同,例如,所有过滤都应在客户端完成。

$("div.modal.edit-category-info").on('shown.bs.modal', function () {
    req = $.ajax({
        url: "some_url",
        method: "GET",
        success: function(data) {
            $('#modalContent').html(data);
            if ( $.fn.dataTable.isDataTable( '#edit-category-table' ) ) {
                editCategoryTable = $('#edit-category-table').DataTable();
            }
            else {
                editCategoryTable = $('#edit-category-table').DataTable( {
                "searching": false,
                "paging": false,
                "ordering": false,
                "bInfo" : false
                } );
            }
            errorValidator("edit-category-table");

            attachFilteringEvents("edit-category-form");
        },
    });
});

I have a button for both tables that triggers the filterTableByErrorClass() function, and I can verify that it gets called in both cases by placing a breakpoint in the first line in there.我有一个触发 filterTableByErrorClass() 函数的两个表的按钮,我可以通过在其中的第一行放置一个断点来验证它在两种情况下都被调用。 If I however place a breakpoint inside the filtering function (inside the filterTableByErrorClass()), that one only gets hit when I filter the first table, and not when I try to filter the modal table.但是,如果我在过滤函数内放置一个断点(在 filterTableByErrorClass() 内),则只有在我过滤第一个表时才会命中该断点,而在我尝试过滤模态表时则不会。

Is $.fn.dataTable.ext.search somehow unaware of the new modal table? $.fn.dataTable.ext.search 是否不知何故不知道新的模态表? Can I make it aware somehow?我能以某种方式让它意识到吗?

As it turns out, the problem was not related to the fact that the modal table was loaded later than the other one.事实证明,问题与模态表加载晚于另一个的事实无关。 The reason for why the filtering did not work on it was the configuration过滤不起作用的原因是配置

"searching": false

which I had applied since there should not be a search box by the modal table.我已经申请了,因为模态表不应该有搜索框。

After some debugging, I noticed that an internal variable in Datatables, bFilter , was set to false and this page explained to me that this is a side-effect of specifying searching as false.经过一些调试后,我注意到 Datatables 中的一个内部变量bFilter被设置为 false,此页面向我解释说这是将searching指定为 false 的副作用。 Not only does it hide the search box, but it also renders the whole table un-searchable.它不仅隐藏了搜索框,而且还使整个表格不可搜索。

To sum up, if I want to get rid of the search box from the table, but I still want to be able to apply my own filtering on the table by other means, I must switch out "searching": false in the configuration of the modal table and replace it with综上所述,如果我想从表格中去掉搜索框,但我仍然希望能够通过其他方式在表格上应用我自己的过滤,我必须在配置中切换掉"searching": false模态表并将其替换为

"dom": 'lrtip'

which just hides the search box (by omitting f from the string) as is explained here and here .它只是隐藏搜索框(通过从字符串中省略 f),如此处和此处所述

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

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