rav
rav

Reputation: 247

DataTables column search for exact match

I am using jQuery DataTables plugin in my application. I am trying to search for a string that exactly matches the data in a column.

I checked the this topic and its solution is not working for my case. My search string is a regular expression with | symbols which might look like Logged In|Active|Not Active.

When the search string contains Active(Logged In|Active), records with Not Active are also showing up when I use the following search:

jQuery("#myTable").DataTable()
                .columns("#status")
                .search("^"+status+"$",true,false)
                .draw();
 

Any help is appreciated. Thanks in advance!

Upvotes: 5

Views: 8236

Answers (3)

Peter Catalin
Peter Catalin

Reputation: 1462

This worked for me:

 $('#yourTableID').DataTable({ 
  search: {
     regex: false,
     smart: false
  }
 })

You can find more information here: https://datatables.net/reference/option/search

Upvotes: 0

rav
rav

Reputation: 247

jQuery("#myTable").DataTable()
                .columns("#status")
                .search("(^"+status+"$)",true,false)
                .draw();

This worked!

Upvotes: 9

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try below solution:
1. with smart search off (reference)

jQuery("#myTable").DataTable()
                .columns("#status")
                .search(status,false)
                .draw();


2.with double quotes (reference)

jQuery("#myTable").DataTable()
                    .columns("#status")
                    .search('"'+status+'"')
                    .draw();

Upvotes: 0

Related Questions