简体   繁体   English

如何从复选框设置列以过滤数据表 jQuery

[英]How to set columns from checkboxes for filtering datatable jQuery

I working with datatables, and i try to make filtering possibility, and problem is that i want to chosse from checkboxes which column i need to filter.我使用数据表,并尝试进行过滤,问题是我想从复选框中选择我需要过滤的列。

So in case if i chosse one column i result i got 4 or 5 column, but i chosse 1, and i can find where the issues.因此,如果我选择一列,我会得到 4 或 5 列,但我选择了 1,我可以找到问题所在。 [![enter image description here][1]][1] [![在此处输入图像描述][1]][1]

As u can see on the image, i chose only one Stantion code but i got 5 columns for filtering, but i must have only one.正如你在图片上看到的,我只选择了一个Stantion 代码,但我有 5 列用于过滤,但我必须只有一列。

My code: [jsfinddle][2]我的代码:[jsfinddle][2]

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);
});

I tried to find anything in the docs related to your question but couldnt find anything.我试图在与您的问题相关的文档中找到任何内容,但找不到任何内容。 To me it seems there is no native way to configure your wanted layout.对我来说,似乎没有本地方式来配置您想要的布局。 It also looks like no matter what targets you define the search pane is rendered for every column.看起来无论您定义什么目标,搜索窗格都会为每一列呈现。 But by knowing that I found a "hacky" way of implementing that feature.但是通过知道我找到了一种实现该功能的“hacky”方式。

Every search pane is put inside a div with the following class: .dtsp-searchPanes每个搜索窗格都放在一个具有以下类的 div 中: .dtsp-searchPanes

Now simply iterate over each child div inside and check if the current iteration is included inside your val array.现在只需遍历内部的每个子 div 并检查当前迭代是否包含在 val 数组中。 If its not: hide the corresponding div otherwise show the div.如果不是:隐藏相应的 div,否则显示 div。

Your addSplitting function should look like this:您的 addSplitting 函数应如下所示:

 function addSpliting(val) { if (val != '') { $("#test1").DataTable({ destroy: true, searchPanes: { layout: 'columns-4', }, columnDefs: [{ searchPanes: { show: true, }, targets: '_all' }], dom: 'Pfrtip' }); $('.dtsp-searchPanes').children().each(function(i, obj) { if (!val.includes(i)) $(this).hide(); else $(this).show(); }); } else { $("#test1").DataTable({ destroy: true }); } }

I checked it several times and it seems to run flawlessly but the whole thing is a little laggy for me.我检查了几次,它似乎运行完美,但整个过程对我来说有点滞后。 Probably not the best way of implementing that feature but the best thing I could come up with.可能不是实现该功能的最佳方式,而是我能想到的最好的方法。

Check the fiddle to see it in action: jsfiddle .检查小提琴以查看它的实际效果: jsfiddle Hope this helps you out.希望这可以帮助你。 If you got any further questions, feel free to ask them!如果您有任何其他问题,请随时问他们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM