列过滤DataTable,去除特定列过滤

Column filtering DataTable, remove specific column filter

我正在使用 DataTables 库中的列过滤器,它为我的 table 中的每一列添加了一个过滤器,问题是我需要从第一列,因为它是一个复选框。我尝试了一些没有成功的东西,我会留下我正在使用的代码。

Link 数据Tables: https://datatables.net/extensions/fixedheader/examples/options/columnFiltering.html

我的Table:

<table id="table" class="table table-sm table-responsive display text-center" style="width:100%">
            <thead>
                <tr>
                    <th><input type="checkbox" id="selectAll" /></th>
                    <th>Id</th>
                    <th>Filial</th>
                    <th>Serie</th>
                    <th>Documento</th>
                    <th>Nop</th>
                </tr>
            </thead>
           
        </table>

JS:

     $(document).ready(function () {
    $("#selectAll").click(function () {
        let select = $(this).is(":checked")
        $("[type=checkbox]").prop('checked', select)
    })
});

$(document).ready(function () {
    $('#table thead tr').clone(true).appendTo('#table thead');
    $('#table thead tr:eq(0) th').each(function (i) {
            $(this).html('<input type="text" />');

            $('input', this).on('keyup change', function () {
                if (table.column(i).search() !== this.value) {
                    table
                        .column(i)
                        .search(this.value)
                        .draw();
                }
            });         
    });

首先要避免绘制控件,请查看以下行:

$('#table thead tr:eq(0) th').each( function (i) { 

这里,i代表一个循环计数器。当计数器为 0 时,您正在为列索引 0(第一列)构建输入控件。因此,您可以在该函数内使用 if(i > 0) { ... } 语句来忽略循环的第一次迭代。

因为您要克隆的标题行已经在第一列中包含一个复选框,您可能还需要使用 $( this ).empty(); 删除“克隆”复选框。

$('#table thead tr:eq(0) th').each(function(i) {

  if (i == 0) {
    $( this ).empty();
  } else {
    var title = $(this).text();

    $(this).html('<input type="text" />');

    $('input', this).on('keyup change', function() {
      if (table.column(i).search() !== this.value) {
        table
          .column(i)
          .search(this.value)
          .draw();
      }
    });
  }

});