DT Searching for multiple values in a column

DT Searching for multiple values in a column

jLinuxjLinux Posts: 981Questions: 73Answers: 75

I was trying to use the column().search() method to search a column for some values, but I wanted to search for multiple values.

I know it takes regex, so I could just do something like table.column(1).search( new RegExp( '(' + values.join('|') + ')' ) ), but I was trying to avoid that. But if thats the only solution, then thats fine

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    I know it takes regex

    It doesn't (yet) actually. You can pass a string in, which has a regular expression in it, but you can't pass in a native RegExp instance (if you can't that isn't documented or supported and only works accidentally!). That is something that will be coming with the next major version of DataTables (although I've just to figure out how to make it work with state saving).

    There are basically two options for what you want at the moment:

    1. As you say - a regex "OR" filter
    2. A custom filter.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    I try to avoid using regex for things like this. I would be ok with writing a custom filter, but Id like to have the search applied only when the search button is clicked. It looks like the filter in that example is executed every time draw() gets called.. The problem with that is if they havent clicked the search button, but something else triggers a draw(), then the filter will be implemented..

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin
    Answer ✓

    Id like to have the search applied only when the search button is clicked

    You would need to have some conditional logic in your filter that would "know" if the button has been pressed or not. Alternatively, you could only add that custom filter to the array when the button is pressed, and remove it again if needed in future.

    Allan

This discussion has been closed.