jQuery DataTables - Filter column by exact match

前端 未结 9 2428
感动是毒
感动是毒 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:23

    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
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-27 18:32

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

    0 讨论(0)
  • 2020-11-27 18:35

    just set the regex and smart false. and you get the exact result.

     $('#yourTableID').DataTable({ 
      search: {
         regex: false,
         smart: false
      }
     })
    
    0 讨论(0)
  • 2020-11-27 18:38

    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.

    0 讨论(0)
  • 2020-11-27 18:41

    If you want the exact match from the beginning you can try this code,

        var table = $('#myTable').DataTable()
        $('#filterrow > th:nth-child(2) > input').on( 'keyup change', function () {
            table
            .column( $(this).parent().index()+':visible' )
            .search( "^" + this.value, true, false, true )
            .draw();
        } );
    
    0 讨论(0)
提交回复
热议问题