Exact Filter Match

Exact Filter Match

Ninja JoeNinja 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

  • kthorngrenkthorngren Posts: 17,270Questions: 25Answers: 4,099

    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 the search() API in regex mode. Something like this:
    http://live.datatables.net/jorexujo/678/edit

    This isn't exactly what you want because Software will match Software Engineer. But Soft 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 the search() API because Datatables performs the search across the full row of data. However it would work with column().search(). See this example.

    Kevin

  • Ninja JoeNinja Joe Posts: 15Questions: 7Answers: 1

    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?

  • kthorngrenkthorngren Posts: 17,270Questions: 25Answers: 4,099
    edited June 16

    Your suggestion worked on the demo but not with my table

    Can you provide an example of what doesn't work? As I suggested the regex pattern might need changed to match your specific requirements.

    Is there a way to do it via the DataTables constructor function options?

    You can enable regex searching with search.regex and disable with search.smart but the user typing in the search string will need to provide the regex tokens.

    search for an exact phrase by enclosing the search text in double quotes.

    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 and search() 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

Sign In or Register to comment.