How do I filter a DataTable programmatically?
How do I filter a DataTable programmatically?
Hello,
I'm experimenting with filtering on DataTables. I'm following along with this article:
https://datatables.net/reference/api/filter()
I've uploaded an exampe of what I'm trying to do here:
http://www.shahspace.com/JQueryDataTable/index.html
My filtering code looks like this:
var filteredData = dt.column(0).data().filter(function(value, index) {
return value == 'A' ? true : false;
``` });
But the grid still shows all values (A's, B's, and C's in the first column).
I'm not sure if I'm supposed to do something with fitleredData, give it to the grid somehow.
Can anyone see what I'm doing wrong?
Answers
The variable
filteredData
contains the filtered results which you can process however you like. It doesn't automatically update the table.You can do something like this to clear the table and apply the filtered data:
So far your examples have been pretty basic but I would recommend using one of the options here to provide test cases. Using these options makes it easier for us to look at and make changes to if needed.
Kevin
Probably worth having a little read over the
filter()
documentation that you linked to again. Specifically it says:The
filter()
method will never effect the display of the records in the table. You need to usesearch()
for that, or custom search.Allan
Thanks both,
allan, I think you're right: search() is really the API function I'm looking for (and it works).
Kevin, your method of clearing the table and supplying the filter results also works, but I get the error described at http://datatables.net/tn/4 (by the sounds of it, it's because the filter object I'm passing in does not match the table DOM object). But I think search() is what I want anyway so it doesn't matter.
And yes, I'll post examples via one of the methods described in that link.