【问题标题】:How to set from checkboxes column for filtering in datatable jQuery?如何从复选框列设置以在数据表 jQuery 中进行过滤?
【发布时间】:2020-09-22 11:16:23
【问题描述】:

我使用 jQuery DataTable 库,我想设置用于从复选框中过滤的列。

在我的例子中,我设置了需要表单复选框的列,如果我选择 I 列,则自动设置为 4,但不设置所需的列。

所以,如果我只选择一列进行过滤,我得到了 4 列,并且没有正确创建过滤器。可能是什么原因?

如上图所示,我设置了站名,但是我没有得到这个过滤器,而是其他4个过滤器,我不明白为什么?

我的代码:JSFiddle

JS:

var idTable = $(".custom-style").attr('id');
var table = $('#' + idTable + '').DataTable();

function addSpliting(val) {
  table.destroy();
  $(idTable).DataTable({
    searchPanes: {
      layout: 'columns-4',
    },
    dom: 'Pfrtip',
    columnDefs: [{
      searchPanes: {
        show: true,
      },
      targets: ['' + val + ''],
    }]
  });
}

function setFilters() {
  $("table thead tr th").each(function(index) {
    var boxes = `<label>
                        <input type="checkbox" class="checkbox" id="${index}"/>
                        ${$(this).text()}
                    </label>`
    if ($(this).text() != "") $(".checkBoxes").append(boxes);
  });
}

setFilters();

$("#createFilter").on("click", function() {
  var checkedIds = $(".checkbox:checked").map(function() {
    return this.id;
  }).toArray();
  addSpliting(checkedIds.join(", "));
});

【问题讨论】:

    标签: javascript jquery datatable


    【解决方案1】:

    实际上,你已经很接近了。问题是 columnDefs' targets 选项只接受以下内容:

    • 整数(正数或负数)
    • 一个字符串(类名)
    • 字符串“_all”(所有列)
    • 一个整数数组

    你正在发送一个 string 数组。

    因此,您只需对代码进行 2 处更改:

    1 - 在 createFilter 的“单击”事件中,创建一个整数数组而不是字符串 1。

    $("#createFilter").on("click", function() {
    
        var columnFilters = [];
    
        $('.checkbox:checked').each(function () {
            columnFilters.push(parseInt($(this).attr('id')));
        });
        addSpliting(columnFilters);
    });
    

    2 - 在你的 jQuery DataTable 初始化中,改变这个:

    targets: ['' + val + '']
    

    用这个:

    targets: val
    

    这是一个工作示例(我对您的代码进行了一些修改):

    addSpliting('');
    
    function addSpliting(val) {
      
      if(val != '') {
          
          $("#test1").DataTable({
            destroy: true,
            searchPanes: {
              layout: 'columns-4',
            },
            dom: 'Pfrtip',
            columnDefs: [{
              searchPanes: {
                show: true,
              },
              targets: val
            }]
          });
       }
       else {
          $("#test1").DataTable({
             destroy: true
          });
       }
    }
    
    function setFilters() {
      
      $("table thead tr th").each(function(index) {
        
        var boxes = `<label>
                    <input type="checkbox" class="checkbox" id="${index}"/>
                    ${$(this).text()}
                    </label>`
        if ($(this).text() != "") $(".checkBoxes").append(boxes);
      });
    }
    
    setFilters();
    
    $("#createFilter").on("click", function() {
       var columFilters = [];
      
       $('.checkbox:checked').each(function () {
          columFilters.push(parseInt($(this).attr('id')));
       });
      
       addSpliting(columFilters);
    });
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/searchpanes/1.2.0/css/searchPanes.dataTables.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/select/1.3.1/css/select.dataTables.min.css">
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/searchpanes/1.2.0/js/dataTables.searchPanes.min.js"></script>
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/select/1.3.1/js/dataTables.select.min.js"></script>
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/scroller/2.0.3/js/dataTables.scroller.min.js"></script>
    
    <div class="content-container">
        <h2 class="head-of-editor">Filtering settings:</h2>
        <div class="checkBoxes"></div>
        <br />
        <a class="button button-clear" id="createFilter" href="#">Create filter</a><br><br>
    </div>
    
    <table class="custom-style" id="test1">
       <thead class="table-head">
          <tr>
             <th>Station code</th>
             <th>Station name</th>
             <th>Station type</th>
             <th>Description</th>
             <th>Belongs to production resource(s)</th>
             <th>Main production resource</th>
             <th></th>
          </tr>
       </thead>
       <tr>
           <td>a</td>
           <td>b</td>
           <td>c</td>
           <td>a</td>
           <td>b</td>
           <td>c</td>
           <td></td>
       </tr>
       <tr>
          <td>a</td>
          <td>b</td>
          <td>c</td>
          <td>a</td>
          <td>b</td>
          <td>c</td>
          <td></td>
       </tr>
       <tr>
          <td>a</td>
          <td>b</td>
          <td>c</td>
          <td>a</td>
          <td>b</td>
          <td>c</td>
          <td></td>
       </tr>
     </table>

    【讨论】:

    • 谢谢你的回答,这里的代码运行良好,但是当我测试它时,我的项目我只选择了一列,结果我得到了 5:D
    • 我尝试以某种方式修复它,但结果收到相同
    • 你认为你可以试试我的代码吗?另外,您能否用您的更改更新您的 jsfiddle 并分享新链接?
    • 在 jsfiddle 中,jsfiddle.net/ryubts8w/7 工作正常,是的,我尝试了您的代码,但结果相同
    • 但是在您的项目中,您是使用原始代码、建议的修改,还是使用我的代码版本?
    猜你喜欢
    • 1970-01-01
    • 2012-10-17
    • 2015-04-15
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2016-09-26
    • 2013-09-16
    相关资源
    最近更新 更多