Filter out data from the datatable based on the selection from the user

时间:2015-07-28 23:45:20

标签: datatables datatables-1.10

Scenario:

I need to filter out data from the datatable based on the selection that has been made from the user. To implement this I have a tree control which represents the hierarchy of the data in the datatable. When a user unchecks a certain node in the tree. That data should be taken out from the datatable.

Question:

  • How do I filter "out" data from the datatable?
  • If I use search() method, it gives me that matched rows and I want the opposite of this. I need to take out the matched rows instead of showing them.
  • I tried using the following filter function but it gives me the filtered data.

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
    

I would appreciate any help on this.

Thanks

2 个答案:

答案 0 :(得分:0)

简单的答案是反转过滤器。即更改过滤器以返回false,其中当前返回true,反之亦然。

您也可以通过反转您要查找的正则表达式来搜索。

答案 1 :(得分:0)

您可以实现自己的自定义搜索功能:



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

$.fn.dataTable.ext.search.push(
  function(oSettings, aData, iDataIndex) {
    var match = true;
    $(".checkbox:checked").each(function(index) {
      var cb = $(this);
      var id = $(this).attr('id');

      if (aData.toString().toLowerCase().indexOf(id) >= 0) {
        match = false;
      } else {
        match = true;
      }
    });
    return match;
  });




请参阅演示here。希望它有所帮助。