Jeff Shain
Jeff Shain

Reputation: 775

Datatables regex search for exact string (not the substring)

Let's say i have a table with three rows. In one column, i have these vaules

  1. coach
  2. Assistant Coach
  3. Coach assistant coach
  4. Coach

If i do a search now for "coach", all rows return. I don't want that. What I need is two sets of code to do the following operations:

1) to be able to type the word "coach" and only have it return rows 1 and 3. 2) to be able to type the word "coach" and only have it return row 1 (case sensitive)

In other words, I don't want any records returned that have the substring coach. Just coach. I haven't been able to figure this out as there is a space between assistant and coach and the regex i have recognizes the free standing word and returns it.

Let me know if this is possible.

My current code is:

var stringFilter = $(this).val();          
var myregex = ("\\b" + stringFilter + "\\b");
$("##add_grid").DataTable().column().columns(7).search(myregex,true,false).draw();

Upvotes: 0

Views: 13513

Answers (3)

satya prakash
satya prakash

Reputation: 97

data table provide more flexibility. to work with table data please find below it will help:

/*column().search( input [,regex[ , smart[ , caseInsen ]]] ) */

 $("#add_grid").DataTable().column(i).search($('#col' + i + '_filter').val(), true,true,false).draw();

Upvotes: 0

CMedina
CMedina

Reputation: 4222

If you want search by exact string you can use column().search() try this code:

dt.column(0).search("^" + "coach" + "$", true, true, false).draw();

Result: https://jsfiddle.net/cmedina/7kfmyw6x/87/

Upvotes: 3

Joel Hernandez
Joel Hernandez

Reputation: 2213

Jeff, try the following code:

$(document).ready( function () {
var table = $('#example').DataTable();
table.column().columns(6).search(myregex,true,false).draw();
} );

Remember that columns starts in 0. Check if your column is in the position #7. Hope this helps!

Upvotes: 0

Related Questions