Javascript DataTables - filter() 函数没有按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27641613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:46:23  来源:igfitidea点击:

Javascript DataTables - filter() function not working as expected

javascriptjquerydatatables

提问by user985723

I am using the DataTables javscript library and I'm trying to filter a row out based on if a numerical value is greater than 60.

我正在使用 DataTables javscript 库,并且我试图根据数值是否大于 60 来过滤出一行。

I'm trying to follow this example: http://datatables.net/reference/api/filter%28%29

我试图按照这个例子:http: //datatables.net/reference/api/filter%28%29

The filter code looks like this:

过滤器代码如下所示:

table
    .column( 3 )
    .data()
    .filter( function ( value, index ) {
        return value > 60 ? true : false;
    } )

The problem is all rows are still visible and no filtering has been done at all. Even if my function simply returns false all rows are still visible. What's going on here?

问题是所有行仍然可见,并且根本没有进行过滤。即使我的函数只是返回 false,所有行仍然可见。这里发生了什么?

JSFiddle of example

示例的 JSFiddle

http://jsfiddle.net/1hLcpr3x/

http://jsfiddle.net/1hLcpr3x/

回答by adeneo

The example you're linking to is filtering the returning array of the data from the columns, not the rows themselves.

您链接到的示例是从列中过滤返回的数据数组,而不是行本身。

You can verify this by returning the content and logging it

您可以通过返回内容并记录来验证这一点

var filteredArray = table.column( 3 )
                         .data()
                         .filter( function(value, index) {
                             return value > 60 ? true : false;
                         })
                         .draw();

console.log(filteredArray);

FIDDLE

小提琴

This is what the filtermethod does, it filters the data when you return it with data(), not the rows.

这就是该filter方法的作用,它在您返回时过滤数据data(),而不是行。

To filter the rows in place, you'd hook into the DataTables plugin, more specifically $.fn.dataTableExt.afnFiltering, and do something like this

要过滤适当的行,您需要连接到 DataTables 插件,更具体地说$.fn.dataTableExt.afnFiltering,并执行以下操作

$.fn.dataTableExt.afnFiltering.push(
    function (oSettings, aData, iDataIndex) {
        return aData[3] < 60;
    }
);

FIDDLE

小提琴

Documentation for DataTables filtering

数据表过滤的文档