Javascript DataTables - filter()函数无法按预期工作

时间:2021-04-12 14:29:49

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/

1 个解决方案

#1


14  

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 filter method does, it filters the data when you return it with data(), not the rows.

这就是过滤器方法的作用,当您使用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

DataTables过滤的文档

#1


14  

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 filter method does, it filters the data when you return it with data(), not the rows.

这就是过滤器方法的作用,当您使用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

DataTables过滤的文档