【发布时间】:2018-06-21 02:06:36
【问题描述】:
我正在尝试使用多个过滤器,这些过滤器将根据选择的过滤器隐藏/显示我的数据表上的行。我的计划是将过滤器值放在一个数组中,并将它们与第一列中的数据搜索属性进行比较,但我目前拥有的不起作用。
这是一个 JSfiddle,我在下面加上代码 https://jsfiddle.net/dmcgrew/06j4pxjk/3/
带有过滤器和表格数据复选框的 HTML..
<label>
<input type="checkbox" name="cat" value="cat" class="filter"> Cats
</label>
<label>
<input type="checkbox" name="dog" value="dog" class="filter"> Dogs
</label>
<table class="select_items">
<thead>
<tr>
<th>Item</th>
<th>Description</th>
<th>Crest Allowed</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr>
<td data-search="cat">1</td>
<td>Testing Bowl</td>
<td>NO</td>
<td><button class="button">Select</button></td>
</tr>
<tr>
<td data-search="dog">32</td>
<td>Cup Test</td>
<td>NO</td>
<td><button class="button">Select</button></td>
</tr>
<tr>
<td data-search="dog">3335</td>
<td>Bowl Test</td>
<td>NO</td>
<td><button class="button">Select</button></td>
</tr>
</tbody>
</table>
JS..
var select_items = $('.select_items').DataTable();
var filters = [];
$('input.filter').on('change', function(){
var filters = [];
$("input.filter:checked").each(function(){
var checkedBox = $(this).val();
if (filters.indexOf(checkedBox) === -1){
filters.push(checkedBox);
}
});
console.log(filters);
if(this.checked){
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex){
return (data[0].indexOf(filters) > -1) ? true : false;
}
);
}
select_items.draw();
if(this.checked){
$.fn.dataTable.ext.search.pop();
}
});
【问题讨论】:
-
不完全。当我同时选择时,我没有得到任何结果。我应该看到所有 3 行。此外,如果我同时选择两个然后取消选择 Dogs,当我应该只看到第一行时,我仍然会看到所有 3 个。
-
您希望过滤器像
or而不是and? -
是的,所以如果有人选择 cat 和 dog,它将显示包含 cat 或 dog 的行。
标签: javascript jquery datatables