最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript DataTables - filter() function not working as expected - Stack Overflow

programmeradmin0浏览0评论

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.

I'm trying to follow this example:

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?

JSFiddle of example

/

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.

I'm trying to follow this example: 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?

JSFiddle of example

http://jsfiddle.net/1hLcpr3x/

Share Improve this question asked Dec 24, 2014 at 19:50 user985723user985723 6282 gold badges8 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

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.

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

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

FIDDLE

Documentation for DataTables filtering

发布评论

评论列表(0)

  1. 暂无评论