Multiple filter value filter
Multiple filter value filter
I've scoured the forum and documentation trying to find a way to filter a table based on a category and an input search term. I want my users to use checkboxes to filter by categories (number) AND use the search box to filter by text. I've gathered the checked checkboxes and have them stored like: "30 29". When I filter using the stored variable, it looks like only an exact string match is returned. Maybe some code would clarify.
Here is my table sample:
[code]
34 30 26 29 28
Cow Boy
34 33 30 29 28
Rodeo
[/code]
Some sample JS
[code]
var active_cats = "30 29";
TableFilter.fnFilter(active_cats, 1);
[/code]
I'm sending "30 29" to fnFilter for the first column only. However only the second row is returned because it appears that the filter is looking for the string "30 29" instead of "30" and "29" individually (in which the first row would be returned as well). How can I filter based on multiple values that may not be in a particular order?
PART 2
On top of returning only entries that pertain to a particular category, I also would like to allow my users to search for keywords. Is it possible to have two "fnFilter" instances without the later only taking affect.
Thanks in advance.
Here is my table sample:
[code]
34 30 26 29 28
Cow Boy
34 33 30 29 28
Rodeo
[/code]
Some sample JS
[code]
var active_cats = "30 29";
TableFilter.fnFilter(active_cats, 1);
[/code]
I'm sending "30 29" to fnFilter for the first column only. However only the second row is returned because it appears that the filter is looking for the string "30 29" instead of "30" and "29" individually (in which the first row would be returned as well). How can I filter based on multiple values that may not be in a particular order?
PART 2
On top of returning only entries that pertain to a particular category, I also would like to allow my users to search for keywords. Is it possible to have two "fnFilter" instances without the later only taking affect.
Thanks in advance.
This discussion has been closed.
Replies
Good question! What you've hit here is a slight difference in the way DataTables handles column filtering and global filtering. They are both done using the fnFilter() API function, with the difference being that the former gives a column integer as the second arguments, but the latter goes not pass a second argument. But this you already know :-)
What you might not know is how DataTables handles the search string.
Column filtering: It's left as it is, So it you search for "30 29" it will look for exactly that string.
Global filtering: The search string is broken up into parts (space separated), so you would get "30 AND 29" (boolean).
So you have a number of options to make column filtering do what you want:
1. Break the string up just like DataTables does for the global filter, into a regular expression, and filter on that - this is the code that DataTables uses to do that:
[code]
var asSearch = bEscapeRegex ?
_fnEscapeRegex( sInput ).split( ' ' ) :
sInput.split( ' ' );
var sRegExpString = '^(?=.*?'+asSearch.join( ')(?=.*?' )+').*$';
[/code]
2. Use a custom filter to do something similar: http://datatables.net/development/filtering
Second part:
You can only have one instance of a filter, but this means that you have one filter for each column, plus the global one. So you could continue your simple column filter, and use the global filter to search for keywords.
Regards,
Allan
I tried working off the example on this page: http://datatables.net/examples/api/regex.html , but it isn't allowing me to filter a specific column with multiple values. For example, if I type in "Gecko" and "KHTML", 23 results should appear, but the query returns an empty result.