How can I use multiple checkbox filter?
How can I use multiple checkbox filter?
Right now, this filter is working when my single checkbox is checked:
$('#sportsNBA').on('change', function() {
if( $(this).is(':checked') ) {
table.columns(1).search('nba');
}
table.draw();
});
However, if I create another one, like so:
$('#sportsNFL').on('change', function() {
if( $(this).is(':checked') ) {
table.columns(1).search('nfl');
}
table.draw();
});
It only takes the second one in consideration.
What I'm basically looking for, is have many checkboxes with filters and allow the user to check as many as he wants and have it filter from the initial data in the table.
UPDATE: I've seen how to make it work with previous versions, but not the current one: http://plnkr.co/edit/b8cLVaVlbNKOQhDwI2mw
This question has an accepted answers - jump to answer
Answers
Thats an interesting example. You need to look at the script.js to see how the search is being executed. The Conversion Guide from 1.9 to 1.10 will help you map to the current naming convention.
fnFilter
maps tosearch()
and the search type is regex. The script is actually usingcolumn().search()
. Usingcolumn().search()
you can setup a similar function to perform a regex search using the checked items.Kevin
But I've tried many things and can't get it to work.
Some filters will be conducted on column 1, some others on column 2, etc...
There are two problems with your code snippets:
nba
will only search fornba
then when clickingnfl
the search will only be fornfl
.I took the code from the working example you posted and updated it for Datatables 1.10 naming convention:
http://live.datatables.net/gesirida/1/edit
HTH,
Kevin
Thank you so much for you time Kevin. I'll have a look
It's working very well. Thanks for your time. I really appreciate it
Great post!
But what if i want the following:
If i check single checkbox to filter the Datatable with values not (nba and nfl).
e.g. show everything except nba and nfl.
in this case,is this necessary??
thank you in advance.
You still need to use regex searches and use
|
to separate the words to remove from the display. You will need anot
type search. Something line this:^((?!nba|nfl).*)$
. I updated the above example to show this with the Position column. The Office column still filters the table showing what is checked.http://live.datatables.net/kopafape/1/edit
Note: The search looks like this
search(positions ? '^((?!' + positions + ').*)$' : '', true, false, false)
. This is to use''
if positions is blank. Otherwise with no checkboxes checked the table will be blank.Kevin
Thank you for the quick reply!
I think i need something easier than that.
I just want the first checkbox (eg. chk1) to remove 'Director' and 'Integration Specialist' (only these 2)
and the second checkbox (eg. chk2) to remove 'New York' and 'San Francisco' (only these 2).
In this case,do i need the following code??
Like I said you will need a regex search. This regex seems to work
^((?!nba|nfl).*)$
. The|
is an or operator for regex. If you want to search for or remove 'Director' or 'Integration Specialist' you will need to use an or search and use|
between search terms. Is this answering your question?Kevin
Yes , thank you so much!
kthorngren considering your answer I follow the example here but in the second checkbox it doesnt filter
someone could help? note that i have 4 checkboxes, which one has the S or N value inside
I answered your same question in this thread:
https://datatables.net/forums/discussion/55956/filtering-datatable-with-checkboxes-column-value-s-appear-value-n-dont#latest
Kevin