Is filter() currently broken?
Is filter() currently broken?
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
Also here's the api documentation I was referring to:
https://datatables.net/reference/api/filter()
No - it will be filtered out of the result set! See this update to your example.
As the documentation for
filter()
notes: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
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
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
Ok gotcha.
datatables is a pretty fantastic library so thank you for all your hard work on it.
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
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...
The
sTableId
property of the settings object is really private - you could usenew $.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
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.
Thanks! Really appreciate it :-)
Regards,
Allan