9

I'm using fnFilter in datatables and while trying to filter "inv" everything else starting with "inv" also get filtered. that is "invc", "invk" are also showing in filtered result. How to solve this and get the exact matches only?

code:

$("#user-lock-status-filter select").change(function() {
        oUserTable.fnFilter($(this).val(), 12);
    });
Davis
  • 310
  • 1
  • 3
  • 14

1 Answers1

16

Change this

oUserTable.fnFilter($(this).val(), 12);

to

oUserTable.fnFilter("^"+$(this).val()+"$", 12, false, false); 
//disabling smart search/regex and apply your own search

Example

Doc

Params for fnFilter

1.{string}: String to filter the table on
2.{int|null}: Column to limit filtering to
3.{bool} [default=false]: Treat as regular expression or not
4.{bool} [default=true]: Perform smart filtering or not
5.{bool} [default=true]: Show the input global filter in it's input box(es)
6.{bool} [default=true]: Do case-insensitive matching (true) or not (false)
bhb
  • 2,476
  • 3
  • 17
  • 32
  • It's not working, I'm still getting "invc" rows in "inv" search. – Davis Oct 01 '13 at 11:44
  • trying to achieve this and quite confused... what is the `^` and `$` for? – Damon Aug 25 '14 at 04:26
  • @Damon, this is the regex (Regular Expression) that will be used to filter the table. ^ means the beginning of the string, and $ means the end of it. So ^searched$ will match lines containing exactly 'searched' only. – Gus Sep 05 '14 at 00:55
  • 1
    Thanks! I think my primary confusion was that you have to set 'treat as regular expression' to false in order treat it as a regular expression... – Damon Sep 05 '14 at 03:19
  • yea, I'm confused by this too.. so why 'treat as regular expression' to false? – tObi May 07 '15 at 14:38
  • 2
    so i think the solution is kind of wrong.. either disable smart filtering and regex and use the normal search string OR write a regex and set 'treat as regular expression' to true – tObi May 07 '15 at 14:42
  • 1
    What is `ouserTable`? think it's a datatable varibale. But i have used `$('.dataTable').dataTable();` to form a jquery datatable. – Avinash Raj Feb 24 '16 at 11:19
  • Confusion expected. These are some of the worst naming conventions in the world. – Damien Roche Jun 10 '19 at 14:56