How can I use multiple checkbox filter?

How can I use multiple checkbox filter?

veryacaveryaca Posts: 11Questions: 4Answers: 0
edited December 2018 in Free community support

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

  • kthorngrenkthorngren Posts: 4,582Questions: 19Answers: 1,007
    edited December 2018

    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 to search() and the search type is regex. The script is actually using column().search(). Using column().search() you can setup a similar function to perform a regex search using the checked items.

    Kevin

  • veryacaveryaca Posts: 11Questions: 4Answers: 0

    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...

  • kthorngrenkthorngren Posts: 4,582Questions: 19Answers: 1,007
    Answer ✓

    There are two problems with your code snippets:

    1. You aren't using regex searching
    2. The searches are executed individually, meaning clicking nba will only search for nba then when clicking nfl the search will only be for nfl.

    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

  • veryacaveryaca Posts: 11Questions: 4Answers: 0

    Thank you so much for you time Kevin. I'll have a look :)

  • veryacaveryaca Posts: 11Questions: 4Answers: 0

    It's working very well. Thanks for your time. I really appreciate it :)

Sign In or Register to comment.