Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript DataTables - filter() function not working as expected

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/

like image 596
user985723 Avatar asked Dec 24 '14 19:12

user985723


People also ask

How fix DataTable is not a function?

To solve the "$(...). DataTable is not a function" jQuery error, make sure to load the jQuery library before loading the DataTables library. The libraries have to be loaded only once on the page, otherwise, the error is thrown.

How do you filter data in a DataTable?

With the DataTable. Select() method, you can directly assign filter rows to an array or data table using this expression. Create a type variable, either DataTable or DataRow [], that is an array of Data Rows. By default, this method returns the array of data rows but you can convert it at any time to a Data Table.


1 Answers

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

like image 196
adeneo Avatar answered Sep 28 '22 15:09

adeneo