Multiple elements in filter

Multiple elements in filter

jLinuxjLinux Posts: 981Questions: 73Answers: 75

I was wondering if there was a way to allow users to input multiple elements the table search, using either a coma and/or a plus sign to separate them.

I know you can do the individual column search, and even allow regex in it, but I mean the filter for the whole table.

Also, I dont necessarily need a full regex search ability, rather just multiple elements that are separated out, meaning I dont want them to have to type something like (something|else) to search for two strings, just something, else or something + else

Thanks!

This question has accepted answers - jump to:

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited September 2015

    Heres my code this far

    $assets_dt.on( 'search.dt', function () {
        var str = $( 'div#data-table_filter' )
            .find('input[type="search"]' )
            .val()
            .replace(/(;|,|\+)\s?/g, "|");
    
        console.log('Searching for ', str);
    
        $assets_dt.search( str ,true, false ).draw();
    } );
    

    But that doesnt work, since it executes a search on each search... turtles all the way down

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    edited September 2015

    Looks to m like it should work. On the reg ex search example page,

    http://datatables.net/examples/api/regex.html

    I put in "acosta|chang" and checked the regex box without smart search and it worked.

    I trust that line 7 is outputting the sort of regex you expect from your search. The problem might be in any spaces carried into the regex.

    " acosta | chang " does not match on the example page.

    Try stripping out spaces

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    That example page is from external search inputs, mines from the search input on datatables..

    If you search, it executes that code, which does a search, which executes that code.. which does a search.... etc

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    D'oh. Of course. Look at your code. You've hooked into the search.dt event and then you call it. You need to move that hook to a keyup on the input box.

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Isnt there a way to just filter the search text?

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    I don't know if there is a way to take control of that native search box. The easiest way around that issue would be to just make a new input box.

  • daniel_rdaniel_r Posts: 460Questions: 4Answers: 67
    Answer ✓

    You can use my yadcf plugin for that too, use initMultipleColumns , it can aacpet text / select / 'multi_select' , you can combine it with the select2 / chosen for a better ui too, see it in action

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited September 2015

    I know I just wanted to have the default search input do it, didnt want to have to hide it and show another one

    @daniel_r I am using it, and it does search for multiple elements in each column, works great!

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    If you really want to leverage the built in input box, you can use JQuery to remove any events tied to it and then apply your own function as an event.

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Hm, very good point.

This discussion has been closed.