如何在过滤器数据表中创建多个条件 jQuery
How to make multiple conditions inside filter datatable jQuery
我使用数据table 库,但遇到问题。在我的页面中,我有过滤菜单,它根据 table header 值生成过滤值。
页面启动时,我过滤数据,只显示活动值,如果用户想查看非活动值,他可以按:
并且用户将看到所有值,但如果我转到主过滤器:
并选择一些值,并尝试使所有值都处于活动状态或非活动状态过滤不起作用。我该如何解决这个问题?
我的过滤代码:
function drawTable(table, className, lenth) {
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
return $(table.row(dataIndex).node()).find(className).length == lenth;
}
);
table.draw();
}
$(document).ready(function () {
var table = $('#' + tableId + '').DataTable();
if ($(".fa").hasClass("fa-eye")) {
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
}
$(".fa-eye-slash").click(function () {
$(".fa-visibles").removeClass('hide');
$(".fa-invisibles").addClass('hide');
$.fn.dataTable.ext.search.pop();
table.draw();
});
$(".fa-eye").click(function () {
$(".fa-visibles").addClass('hide');
$(".fa-invisibles").removeClass('hide');
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
});
});
当你点击过滤图标时,选择例如“Office -> Regional Director”,table 3 个值,但只显示两个,我想取消此过滤器并点击 eye,但这不起作用。
您要求提供示例 - 我假设您指的是使用我提到的窗格技术的示例,因此我在下面提供了一个示例。不过,我仍然认为您可能需要考虑使用 search builder。
我的示例执行以下操作:
(1) 它为第一列使用一个窗格。 table 列显示“复选”和“叉号”符号,但筛选器窗格使用单词“活动”和“非活动”。
(2) 打开 table,开始时只选择“活动”记录。
我做了以下修改:
(a) 将 HTML 源数据更改为使用 <td>active</td>
和 <td>inactive</td>
,而不是在 HTML table 中嵌入 fontawesome 符号。
(b) 添加了渲染函数来处理第一列在 table 中的显示方式(使用 FA 符号而不是文字)。
(c) 添加了处理搜索窗格如何在第一列中使用此数据的逻辑。
(d) 仅预选“活动”记录。
我删除了你所有其他逻辑。
这意味着该页面会显示每个搜索窗格(仅用于我的演示)- 因此您可能想要 恢复您的逻辑以隐藏和显示特定的搜索窗格 ,根据需要,使用您的复选框。否则窗格在页面中占用太多 space(在我的演示中)。
这是演示:
$(document).ready(function() {
var table = $('#example').DataTable( {
dom: 'Plfrtip',
columnDefs: [
{
targets: 0, // the first column needs custom handling:
render: function ( data, type, row, meta ) {
if ( type === 'display' ) { // show symbols, instead of table data:
if (data === 'active') {
return '<em class="fa fa-check pointer fa-active"></em>';
} else {
return '<em class="fa fa-close pointer fa-inactive"></em>';
}
} else { // all other types, including "filter" and "sort"
return data;
}
},
searchPanes: {
show: true,
orthogonal: {
// For the search pane, use the "filter" data from the above
// render function. In this case, that is the original raw
// data from the table.
display: 'filter'
},
preSelect: ['active']
}
},
{
targets: "_all", // all remaining filter panes are also shown:
searchPanes: {
show: true,
initCollapsed: true
}
}
]
} );
} );
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
<!--
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
<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://datatables.net/media/css/site-examples.css">
-->
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/select/1.3.0/js/dataTables.select.min.js"></script>
<script src="https://cdn.datatables.net/searchpanes/1.0.1/js/dataTables.searchPanes.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/searchpanes/1.0.1/css/searchPanes.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/select/1.3.0/css/select.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">
</head>
<body>
<div style="margin: 20px;">
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Status</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td> inactive</td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>0,800</td>
</tr>
<tr>
<td>active</td>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>0,750</td>
</tr>
<tr>
<td>active</td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>,000</td>
</tr>
<tr>
<td> inactive</td>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>3,060</td>
</tr>
<tr>
<td>active</td>
<td>Airi Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>2,700</td>
</tr>
<tr>
<td>active</td>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>2,000</td>
</tr>
<tr>
<td>active</td>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012/08/06</td>
<td>7,500</td>
</tr>
<tr>
<td>active</td>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010/10/14</td>
<td>7,900</td>
</tr>
<tr>
<td>active</td>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td>39</td>
<td>2009/09/15</td>
<td>5,500</td>
</tr>
<tr>
<td>active</td>
<td>Sonya Frost</td>
<td>Software Engineer</td>
<td>Edinburgh</td>
<td>23</td>
<td>2008/12/13</td>
<td>3,600</td>
</tr>
<tr>
<td>active</td>
<td>Jena Gaines</td>
<td>Office Manager</td>
<td>London</td>
<td>30</td>
<td>2008/12/19</td>
<td>,560</td>
</tr>
<tr>
<td>active</td>
<td>Quinn Flynn</td>
<td>Support Lead</td>
<td>Edinburgh</td>
<td>22</td>
<td>2013/03/03</td>
<td>2,000</td>
</tr>
<tr>
<td>active</td>
<td>Charde Marshall</td>
<td>Regional Director</td>
<td>San Francisco</td>
<td>36</td>
<td>2008/10/16</td>
<td>0,600</td>
</tr>
<tr>
<td>inactive</td>
<td>Haley Kennedy</td>
<td>Senior Marketing Designer</td>
<td>London</td>
<td>43</td>
<td>2012/12/18</td>
<td>3,500</td>
</tr>
<tr>
<td>inactive</td>
<td>Tatyana Fitzpatrick</td>
<td>Regional Director</td>
<td>London</td>
<td>19</td>
<td>2010/03/17</td>
<td>5,750</td>
</tr>
<tr>
<td>inactive</td>
<td>Michael Silva</td>
<td>Marketing Designer</td>
<td>London</td>
<td>66</td>
<td>2012/11/27</td>
<td>8,500</td>
</tr>
<tr>
<td> inactive</td>
<td>Paul Byrd</td>
<td>Chief Financial Officer (CFO)</td>
<td>New York</td>
<td>64</td>
<td>2010/06/09</td>
<td>5,000</td>
</tr>
<tr>
<td> inactive</td>
<td>Gloria Little</td>
<td>Systems Administrator</td>
<td>New York</td>
<td>59</td>
<td>2009/04/10</td>
<td>7,500</td>
</tr>
<tr>
<td> inactive</td>
<td>Bradley Greer</td>
<td>Software Engineer</td>
<td>London</td>
<td>41</td>
<td>2012/10/13</td>
<td>2,000</td>
</tr>
<tr>
<td>inactive</td>
<td>Dai Rios</td>
<td>Personnel Lead</td>
<td>Edinburgh</td>
<td>35</td>
<td>2012/09/26</td>
<td>7,500</td>
</tr>
<tr>
<td>active</td>
<td>Jenette Caldwell</td>
<td>Development Lead</td>
<td>New York</td>
<td>30</td>
<td>2011/09/03</td>
<td>5,000</td>
</tr>
<tr>
<td>active</td>
<td>Yuri Berry</td>
<td>Chief Marketing Officer (CMO)</td>
<td>New York</td>
<td>40</td>
<td>2009/06/25</td>
<td>5,000</td>
</tr>
<tr>
<td>active</td>
<td>Caesar Vance</td>
<td>Pre-Sales Support</td>
<td>New York</td>
<td>21</td>
<td>2011/12/12</td>
<td>6,450</td>
</tr>
<tr>
<td>active</td>
<td>Doris Wilder</td>
<td>Sales Assistant</td>
<td>Sydney</td>
<td>23</td>
<td>2010/09/20</td>
<td>,600</td>
</tr>
<tr>
<td>active</td>
<td>Angelica Ramos</td>
<td>Chief Executive Officer (CEO)</td>
<td>London</td>
<td>47</td>
<td>2009/10/09</td>
<td>,200,000</td>
</tr>
<tr>
<td>active</td>
<td>Gavin Joyce</td>
<td>Developer</td>
<td>Edinburgh</td>
<td>42</td>
<td>2010/12/22</td>
<td>,575</td>
</tr>
<tr>
<td>active</td>
<td>Jennifer Chang</td>
<td>Regional Director</td>
<td>Singapore</td>
<td>28</td>
<td>2010/11/14</td>
<td>7,650</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
根据您的代码 JSFiddle,您的所有代码都运行良好。多重条件不起作用,因为你的逻辑是错误的。
让我解释一下。
活动和非活动条件正在处理当前 table 的数据。
首先,table的数据有所有数据(活跃的和不活跃的)。你可以
像 "Office -> Regional Director" 这样的筛选器,你可以找到 3
记录。然后您可以单击活动和非活动条件事件。
它会正常工作。
其次,你隐藏了一些不活跃的数据
table,它在你的 table.And 中只有活动数据,然后,你过滤
例如 “办公室 -> 区域总监”。你只能找到2条记录,
无法显示非活动记录,因为当前 table 的数据没有
非活动数据。
解决方案:
- 你应该在调用过滤器函数时调整你的代码,你必须先重新加载所有 table 数据,然后你才能使用所有数据调用过滤器进程。
- 完成筛选数据后,您必须检查活动或非活动条件。
示例代码,基于您提供的代码。希望大家理解。
let tableId = "example";
$('.filtering-system').hide();
$('.button-container').hide();
addSpliting('');
function showAllVaues() {
$('#' + tableId + '').dataTable().api().pagae.len(-1).draw();
}
function addSpliting(val, length) {
if (val != '') {
//start Reload- Before Filter event, need to reload all data
var table = $('#' + tableId + '').DataTable();
$.fn.dataTable.ext.search.pop();
table.draw();
//end Reload
$('#' + tableId + '').DataTable({
destroy: true,
searchPanes: {
layout: 'columns-' + length + ''
},
columnDefs: [{
searchPanes: {
show: true
},
targets: '_all'
}],
dom: 'Pfrtip'
});
$('.dtsp-searchPanes').children().each(function (i, obj) {
if (!val.includes(i)) $(this).hide();
else $(this).show();
});
//start check - When get filter data, need to check active or not
if ($(".fa").hasClass("fa-eye")) {
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
}
//end check
} else {
$('#' + tableId + '').DataTable({
destroy: true
});
}
}
function setFilters() {
$("table thead tr th").each(function (index) {
var boxes = `<label>
<input type="checkbox" class="custom-checkbox" id="${index}"/>
${$(this).text()}
</label>`;
if ($(this).text() != "" && $(this).hasClass('checkbox-mark') === false) $(".checkBoxes").append(boxes);
});
}
setFilters();
$("#createFilter").on("click", function () {
var columFilters = [];
$('.custom-checkbox:checked').each(function () {
columFilters.push(parseInt($(this).attr('id')));
});
addSpliting(columFilters, columFilters.length);
});
$("#fil-sys").on("click", function () {
$('.filtering-system').slideToggle('slow');
});
function drawTable(table, className, lenth) {
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
return $(table.row(dataIndex).node()).find(className).length == lenth;
}
);
table.draw();
}
$(document).ready(function () {
var table = $('#' + tableId + '').DataTable();
if ($(".fa").hasClass("fa-eye")) {
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
}
$(".fa-eye-slash").click(function () {
$(".fa-visibles").removeClass('hide');
$(".fa-invisibles").addClass('hide');
$.fn.dataTable.ext.search.pop();
table.draw();
});
$(".fa-eye").click(function () {
$(".fa-visibles").addClass('hide');
$(".fa-invisibles").removeClass('hide');
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
});
});
我使用数据table 库,但遇到问题。在我的页面中,我有过滤菜单,它根据 table header 值生成过滤值。
页面启动时,我过滤数据,只显示活动值,如果用户想查看非活动值,他可以按:
并且用户将看到所有值,但如果我转到主过滤器:
并选择一些值,并尝试使所有值都处于活动状态或非活动状态过滤不起作用。我该如何解决这个问题?
我的过滤代码:
function drawTable(table, className, lenth) {
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
return $(table.row(dataIndex).node()).find(className).length == lenth;
}
);
table.draw();
}
$(document).ready(function () {
var table = $('#' + tableId + '').DataTable();
if ($(".fa").hasClass("fa-eye")) {
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
}
$(".fa-eye-slash").click(function () {
$(".fa-visibles").removeClass('hide');
$(".fa-invisibles").addClass('hide');
$.fn.dataTable.ext.search.pop();
table.draw();
});
$(".fa-eye").click(function () {
$(".fa-visibles").addClass('hide');
$(".fa-invisibles").removeClass('hide');
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
});
});
当你点击过滤图标时,选择例如“Office -> Regional Director”,table 3 个值,但只显示两个,我想取消此过滤器并点击 eye,但这不起作用。
您要求提供示例 - 我假设您指的是使用我提到的窗格技术的示例,因此我在下面提供了一个示例。不过,我仍然认为您可能需要考虑使用 search builder。
我的示例执行以下操作:
(1) 它为第一列使用一个窗格。 table 列显示“复选”和“叉号”符号,但筛选器窗格使用单词“活动”和“非活动”。
(2) 打开 table,开始时只选择“活动”记录。
我做了以下修改:
(a) 将 HTML 源数据更改为使用 <td>active</td>
和 <td>inactive</td>
,而不是在 HTML table 中嵌入 fontawesome 符号。
(b) 添加了渲染函数来处理第一列在 table 中的显示方式(使用 FA 符号而不是文字)。
(c) 添加了处理搜索窗格如何在第一列中使用此数据的逻辑。
(d) 仅预选“活动”记录。
我删除了你所有其他逻辑。
这意味着该页面会显示每个搜索窗格(仅用于我的演示)- 因此您可能想要 恢复您的逻辑以隐藏和显示特定的搜索窗格 ,根据需要,使用您的复选框。否则窗格在页面中占用太多 space(在我的演示中)。
这是演示:
$(document).ready(function() {
var table = $('#example').DataTable( {
dom: 'Plfrtip',
columnDefs: [
{
targets: 0, // the first column needs custom handling:
render: function ( data, type, row, meta ) {
if ( type === 'display' ) { // show symbols, instead of table data:
if (data === 'active') {
return '<em class="fa fa-check pointer fa-active"></em>';
} else {
return '<em class="fa fa-close pointer fa-inactive"></em>';
}
} else { // all other types, including "filter" and "sort"
return data;
}
},
searchPanes: {
show: true,
orthogonal: {
// For the search pane, use the "filter" data from the above
// render function. In this case, that is the original raw
// data from the table.
display: 'filter'
},
preSelect: ['active']
}
},
{
targets: "_all", // all remaining filter panes are also shown:
searchPanes: {
show: true,
initCollapsed: true
}
}
]
} );
} );
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
<!--
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
<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://datatables.net/media/css/site-examples.css">
-->
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/select/1.3.0/js/dataTables.select.min.js"></script>
<script src="https://cdn.datatables.net/searchpanes/1.0.1/js/dataTables.searchPanes.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/searchpanes/1.0.1/css/searchPanes.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/select/1.3.0/css/select.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">
</head>
<body>
<div style="margin: 20px;">
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Status</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td> inactive</td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>0,800</td>
</tr>
<tr>
<td>active</td>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>0,750</td>
</tr>
<tr>
<td>active</td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>,000</td>
</tr>
<tr>
<td> inactive</td>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>3,060</td>
</tr>
<tr>
<td>active</td>
<td>Airi Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>2,700</td>
</tr>
<tr>
<td>active</td>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>2,000</td>
</tr>
<tr>
<td>active</td>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012/08/06</td>
<td>7,500</td>
</tr>
<tr>
<td>active</td>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010/10/14</td>
<td>7,900</td>
</tr>
<tr>
<td>active</td>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td>39</td>
<td>2009/09/15</td>
<td>5,500</td>
</tr>
<tr>
<td>active</td>
<td>Sonya Frost</td>
<td>Software Engineer</td>
<td>Edinburgh</td>
<td>23</td>
<td>2008/12/13</td>
<td>3,600</td>
</tr>
<tr>
<td>active</td>
<td>Jena Gaines</td>
<td>Office Manager</td>
<td>London</td>
<td>30</td>
<td>2008/12/19</td>
<td>,560</td>
</tr>
<tr>
<td>active</td>
<td>Quinn Flynn</td>
<td>Support Lead</td>
<td>Edinburgh</td>
<td>22</td>
<td>2013/03/03</td>
<td>2,000</td>
</tr>
<tr>
<td>active</td>
<td>Charde Marshall</td>
<td>Regional Director</td>
<td>San Francisco</td>
<td>36</td>
<td>2008/10/16</td>
<td>0,600</td>
</tr>
<tr>
<td>inactive</td>
<td>Haley Kennedy</td>
<td>Senior Marketing Designer</td>
<td>London</td>
<td>43</td>
<td>2012/12/18</td>
<td>3,500</td>
</tr>
<tr>
<td>inactive</td>
<td>Tatyana Fitzpatrick</td>
<td>Regional Director</td>
<td>London</td>
<td>19</td>
<td>2010/03/17</td>
<td>5,750</td>
</tr>
<tr>
<td>inactive</td>
<td>Michael Silva</td>
<td>Marketing Designer</td>
<td>London</td>
<td>66</td>
<td>2012/11/27</td>
<td>8,500</td>
</tr>
<tr>
<td> inactive</td>
<td>Paul Byrd</td>
<td>Chief Financial Officer (CFO)</td>
<td>New York</td>
<td>64</td>
<td>2010/06/09</td>
<td>5,000</td>
</tr>
<tr>
<td> inactive</td>
<td>Gloria Little</td>
<td>Systems Administrator</td>
<td>New York</td>
<td>59</td>
<td>2009/04/10</td>
<td>7,500</td>
</tr>
<tr>
<td> inactive</td>
<td>Bradley Greer</td>
<td>Software Engineer</td>
<td>London</td>
<td>41</td>
<td>2012/10/13</td>
<td>2,000</td>
</tr>
<tr>
<td>inactive</td>
<td>Dai Rios</td>
<td>Personnel Lead</td>
<td>Edinburgh</td>
<td>35</td>
<td>2012/09/26</td>
<td>7,500</td>
</tr>
<tr>
<td>active</td>
<td>Jenette Caldwell</td>
<td>Development Lead</td>
<td>New York</td>
<td>30</td>
<td>2011/09/03</td>
<td>5,000</td>
</tr>
<tr>
<td>active</td>
<td>Yuri Berry</td>
<td>Chief Marketing Officer (CMO)</td>
<td>New York</td>
<td>40</td>
<td>2009/06/25</td>
<td>5,000</td>
</tr>
<tr>
<td>active</td>
<td>Caesar Vance</td>
<td>Pre-Sales Support</td>
<td>New York</td>
<td>21</td>
<td>2011/12/12</td>
<td>6,450</td>
</tr>
<tr>
<td>active</td>
<td>Doris Wilder</td>
<td>Sales Assistant</td>
<td>Sydney</td>
<td>23</td>
<td>2010/09/20</td>
<td>,600</td>
</tr>
<tr>
<td>active</td>
<td>Angelica Ramos</td>
<td>Chief Executive Officer (CEO)</td>
<td>London</td>
<td>47</td>
<td>2009/10/09</td>
<td>,200,000</td>
</tr>
<tr>
<td>active</td>
<td>Gavin Joyce</td>
<td>Developer</td>
<td>Edinburgh</td>
<td>42</td>
<td>2010/12/22</td>
<td>,575</td>
</tr>
<tr>
<td>active</td>
<td>Jennifer Chang</td>
<td>Regional Director</td>
<td>Singapore</td>
<td>28</td>
<td>2010/11/14</td>
<td>7,650</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
根据您的代码 JSFiddle,您的所有代码都运行良好。多重条件不起作用,因为你的逻辑是错误的。
让我解释一下。
活动和非活动条件正在处理当前 table 的数据。
首先,table的数据有所有数据(活跃的和不活跃的)。你可以 像 "Office -> Regional Director" 这样的筛选器,你可以找到 3 记录。然后您可以单击活动和非活动条件事件。 它会正常工作。
其次,你隐藏了一些不活跃的数据 table,它在你的 table.And 中只有活动数据,然后,你过滤 例如 “办公室 -> 区域总监”。你只能找到2条记录, 无法显示非活动记录,因为当前 table 的数据没有 非活动数据。
解决方案:
- 你应该在调用过滤器函数时调整你的代码,你必须先重新加载所有 table 数据,然后你才能使用所有数据调用过滤器进程。
- 完成筛选数据后,您必须检查活动或非活动条件。
示例代码,基于您提供的代码。希望大家理解。
let tableId = "example";
$('.filtering-system').hide();
$('.button-container').hide();
addSpliting('');
function showAllVaues() {
$('#' + tableId + '').dataTable().api().pagae.len(-1).draw();
}
function addSpliting(val, length) {
if (val != '') {
//start Reload- Before Filter event, need to reload all data
var table = $('#' + tableId + '').DataTable();
$.fn.dataTable.ext.search.pop();
table.draw();
//end Reload
$('#' + tableId + '').DataTable({
destroy: true,
searchPanes: {
layout: 'columns-' + length + ''
},
columnDefs: [{
searchPanes: {
show: true
},
targets: '_all'
}],
dom: 'Pfrtip'
});
$('.dtsp-searchPanes').children().each(function (i, obj) {
if (!val.includes(i)) $(this).hide();
else $(this).show();
});
//start check - When get filter data, need to check active or not
if ($(".fa").hasClass("fa-eye")) {
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
}
//end check
} else {
$('#' + tableId + '').DataTable({
destroy: true
});
}
}
function setFilters() {
$("table thead tr th").each(function (index) {
var boxes = `<label>
<input type="checkbox" class="custom-checkbox" id="${index}"/>
${$(this).text()}
</label>`;
if ($(this).text() != "" && $(this).hasClass('checkbox-mark') === false) $(".checkBoxes").append(boxes);
});
}
setFilters();
$("#createFilter").on("click", function () {
var columFilters = [];
$('.custom-checkbox:checked').each(function () {
columFilters.push(parseInt($(this).attr('id')));
});
addSpliting(columFilters, columFilters.length);
});
$("#fil-sys").on("click", function () {
$('.filtering-system').slideToggle('slow');
});
function drawTable(table, className, lenth) {
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
return $(table.row(dataIndex).node()).find(className).length == lenth;
}
);
table.draw();
}
$(document).ready(function () {
var table = $('#' + tableId + '').DataTable();
if ($(".fa").hasClass("fa-eye")) {
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
}
$(".fa-eye-slash").click(function () {
$(".fa-visibles").removeClass('hide');
$(".fa-invisibles").addClass('hide');
$.fn.dataTable.ext.search.pop();
table.draw();
});
$(".fa-eye").click(function () {
$(".fa-visibles").addClass('hide');
$(".fa-invisibles").removeClass('hide');
drawTable(table, '.fa-inactive, .fa-invisible-data', 0);
});
});