jQuery DataTables - Filter column by exact match

前端 未结 9 2487
感动是毒
感动是毒 2020-11-27 18:06

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 m

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 18:30

    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!

提交回复
热议问题