DataTables使用固定行进行过滤

时间:2018-06-11 13:43:34

标签: html-table datatables filtering

我有一个带有DataTables插件的表的简化结构:

<table class="tasks-datatable">
   <thead>
      <tr>
         <td>Head title</td>
      </tr>
   </thead>
   <tbody>
      <tr class="head-row">
         <td>Category 1</td>
      </tr>
      <tr>
         <td>Subcategory 1</td>
      </tr>
      <tr>
         <td>Subcategory 2</td>
      </tr>
      <tr class="head-row">
         <td>Category 2</td>
      </tr>
      <tr>
         <td>Subcategory 3</td>
      </tr>
      <tr>
         <td>Subcategory 4</td>
      </tr>
   </tbody>
</table>

我启用了bFilter过滤,但我只需要过滤带子类别的行并保留所有类别行。因此,如果我输入“Subcategory 1”来过滤输入,结果应如下所示:

<table class="tasks-datatable">
   <thead>
      <tr>
         <td>Head title</td>
      </tr>
   </thead>
   <tbody>
      <tr class="head-row">
         <td>Category 1</td>
      </tr>
      <tr>
         <td>Subcategory 1</td>
      </tr>
      <tr class="head-row"> <!-- It doesn't matter if this will be visible if it has no subcategories -->
         <td>Category 2</td>
      </tr>
   </tbody>
</table>

有可能吗?我的JS代码看起来像这样,但当我输入一些东西来过滤输入时,类别行被视为子类别行。

$('.tasks-datatable').each(function () {
    var options = {
        language: {
            url: '/assets/js/datatable-langs/czech.json'
        },
        lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "Všechny"]],
        pageLength: -1,
        fixedColumns: true,
        scrollCollapse: true,
        scrollX: true,
        columnDefs: [{width: 150, targets: 0}],
        bFilter: true,
        scrollY: '60vh',
        paging: false,
        bSort: false
    };

    var table = $(this).DataTable(options);
});

感谢。

0 个答案:

没有答案