Exact Filter Match
Exact Filter Match
Ninja Joe
Posts: 15Questions: 7Answers: 1
I have a DataTable I'm initializing with:
$( '#builders-datatable' ).DataTable( { scrollX: true, pageLength: 50, pagingType: 'full_numbers' } );
It works great! But I'd like the Search filter to show exact matches only.
For example, if I enter:
BROOKCAL
Only the following should be displayed:
BROOKCAL
Currently, it's displaying:
BROOKCAL
BROOKCAL ONTARIO
BROOKCAL RANCHO
Answers
Read about the different search options at the
search()
API docs. Sounds like you want to use regex searching. See this example. One option would be to take over the search input event handler and use thesearch()
API in regex mode. Something like this:http://live.datatables.net/jorexujo/678/edit
This isn't exactly what you want because
Software
will matchSoftware Engineer
. ButSoft
will only match Ashton Cox. You can change the regex expression to more closely meet your needs. Using"^" + this.value.trim() + "$"
won't work with thesearch()
API because Datatables performs the search across the full row of data. However it would work withcolumn().search()
. See this example.Kevin
Your suggestion worked on the demo but not with my table. Is there a way to do it via the DataTables constructor function options?
Also, I went to:
https://datatables.net/reference/api/search()
It says:
DataTables 1.10 adds the ability to search for an exact phrase by enclosing the search text in double quotes.
I'm using version 1.11.3 but using double quotes didn't yield an exact match for me. What gives?
Can you provide an example of what doesn't work? As I suggested the regex pattern might need changed to match your specific requirements.
You can enable regex searching with
search.regex
and disable withsearch.smart
but the user typing in the search string will need to provide the regex tokens.Looking at the example provided I believe the intention is to not match out of order words. Searching for
"san"
will still match "San Francisco". The global search input andsearch()
execute the search across the whole row not each column individually. Meaning it takes all the columns in the row and combines them into a string to perform the search.You can also create a search plugin to perform the searches the way you want. In the plugin you can take the global search input value and perform an exact word search individually across all the columns.
Kevin