jQuery DataTables - Filter column by exact match

2019-01-06 16:43发布

Trying to only display exact matches to the search term entered in the search bar.

For instance, I have a search bar that filters by ID#. I want only records that match the exact # entered to display.

So if 123 is entered, I don't want 12345, 91239, etc etc to be displayed. Only 123.

Saw some info about bRegex on the FAQ page, but it's not working for me. Any ideas?

8条回答
等我变得足够好
2楼-- · 2019-01-06 16:57

You can use regular expression for exact matching as following:

var table = $('#dt').DataTable();

$('#column3_search').on('keyup', function () {
    // Note: column() accepts zero-based index meaning the index of first column is 0, second column is 1 and so on.
    // We use `2` here as we are accessing 3rd column whose index is 2.
    table.column(2)
         .search("^" + this.value + "$", true, false, true)
         .draw();
});

The syntax of the search function is:

search(input, regex, smart_search, case_insensitive)

We disable smart search in this case because search function uses regular expression internally when smart search is set to true. Otherwise, this creates conflict between our regular expression and the one that is used by search function.

For more information, check out the following documentation from DataTable:

column().search()

Hope it's helpful!

查看更多
Anthone
3楼-- · 2019-01-06 16:58
$(document).ready( function() {
    $('#example').dataTable( {
        "oSearch": {"bSmart": false}
    } );
} )

Try using the bSmart option and setting it to false

From the documentation

"When "bSmart" DataTables will use it's smart filtering methods (to word match at any point in the data), when false this will not be done."

UPDATE

I found this:

oSettings.aoPreSearchCols[ iCol ].sSearch = "^\\s*"+'1'+"\\s*$";
oSettings.aoPreSearchCols[ iCol ].bRegex = false;
oSettings.aoPreSearchCols[ iCol ].bSmart= false;

at this link http://www.datatables.net/forums/discussion/4096/filtering-an-exact-match/p1

looks like you can set bSmart and bRegex per column as well as specifying a manual regex per column.

查看更多
唯我独甜
4楼-- · 2019-01-06 17:00

Ok solved the problem. However, since the column I am using the exact match on sometimes contains multiple ID #s seperated by commas, I wont be able to use an exact match search.

But for those interested, here is the answer:

oTable.fnFilter( "^"+TERM+"$", COLUMN , true); //Term, Column #, RegExp Filter
查看更多
forever°为你锁心
5楼-- · 2019-01-06 17:03

The current versions of Datatables supports using real exact matching on a column basis.

table.column(i)
.search($(this).val(), false, false, false)
.draw();

The documentation explains each flag.

查看更多
时光不老,我们不散
6楼-- · 2019-01-06 17:08
$(document).ready(function() {
    tbl = $('#example').dataTable();
    tbl.fnFilter("^" + filter_value + "$");
});

Where filter_value is the string entered in the filter field.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-06 17:08

table.column(col_num).search(filter_value + "$", true, true, false).draw();

查看更多
登录 后发表回答