Is filter() currently broken?

Is filter() currently broken?

spaceguyspaceguy Posts: 6Questions: 0Answers: 0

Per the API documentation, I've been trying to get the filter() method working without success.

See:
http://live.datatables.net/qoqocoge/1/edit

The function is supposed to be called for each row in the table. That is working correctly as you can see from the logs in the console. But as I understand it any row for which the function returns "false" show be filtered out of the table. This is not happening. In the example I simply put "return false" so that ever row should be filtered out, but no luck.

Any suggestions on how I can get this to work?

Replies

  • spaceguyspaceguy Posts: 6Questions: 0Answers: 0

    Also here's the api documentation I was referring to:
    https://datatables.net/reference/api/filter()

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    edited November 2014

    But as I understand it any row for which the function returns "false" show be filtered out of the table.

    No - it will be filtered out of the result set! See this update to your example.

    As the documentation for filter() notes:

    The filter() method provides a way of filtering out content in an API instance's result set which does not pass the criteria set by the provided callback method. This method should not be confused with search() which is used to search for records in the DataTable.

    You want to use search() if you want to search the table and update the display of the table.

    There is, however, currently no method of giving a function to search() - this will come with a future update. If you need custom searching beyond what is built into DataTables you would need to create a plug-in.

    Allan

  • spaceguyspaceguy Posts: 6Questions: 0Answers: 0
    edited November 2014

    Ok thanks for the quick response.

    Is there a way to update the table with the new result set after a filter()? Say I want to do a simple filter in the example to only show entries where the person is older than 30:
    http://live.datatables.net/qoqocoge/5/edit

    A function of

    return value > 30 ? true : false;

    should filter the result set down to only those entries older than 30. But calling draw() does not redraw the table, is there some other way to update the table with the new result set?

    Thanks again

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    No - as I noted you would need to use a custom filter plug-in. There is an example here.

    Ultimately yes this should be possible, but it a feature still to be developed!

    Allan

  • spaceguyspaceguy Posts: 6Questions: 0Answers: 0

    Ok gotcha.

    datatables is a pretty fantastic library so thank you for all your hard work on it.

  • spaceguyspaceguy Posts: 6Questions: 0Answers: 0

    Ok I've successfully implemented a custom search function using

    $.fn.dataTable.ext.search.push( function() ...)

    as in the example you linked to. But now that filter is being run on both of my datatables! (I have two datatables in the view, which are created separately from two different data sources).

    Any suggestions on the best way to keep these custom search functions (each datatable will have several) separate to each table?

    Thanks again

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Yes that's a real annoyance of the filtering plug-ins at the moment that they are global.

    You need to do check if the filter request is for the table you actually want to filter...

    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
           if ( settings.sTableId !== 'myFilteringTable' ) {
              return true;
           }
    
           // filtering logic
        }
    );
    

    The sTableId property of the settings object is really private - you could use new $.fn.dataTable.Api( settings ).table().node().id to get the same information using the public API, and possibly I should be recommending that - but I know that property is there and available... It likely won't be in DataTables v2, but that is likely years away!

    Filtering is in desperate need of a rewrite in DataTables (or rather than API for plug-ins and options, such as passing in a custom function for search()). That is the plan for 1.11.

    Allan

  • spaceguyspaceguy Posts: 6Questions: 0Answers: 0

    Ok thanks, that makes sense and it worked like a charm!

    I should say that I did some hacking on dataTables 1.9.4 and I really like the new API, its quite clean and straight-forward to use. Really don't know how I'd do this new project without it.

    I have some more questions / things for discussion but will post them as new discussion threads.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    I should say that I did some hacking on dataTables 1.9.4 and I really like the new API, its quite clean and straight-forward to use.

    Thanks! Really appreciate it :-)

    Regards,
    Allan

This discussion has been closed.