[英]Resetting my multi select filter for a JQuery Datatable
For my application, I used this example: JQuery Datatable column filtering using multi-select options 对于我的应用程序,我使用以下示例: 使用多选选项的JQuery Datatable列过滤 . 。 Huge thanks to the author and the one who answered btw! 非常感谢作者和回答问题的人!
Now, I tweaked it using my own data and included my own data. 现在,我使用自己的数据对其进行了调整,并包含了自己的数据。 It's working fine but I wanted to add an additional feature. 工作正常,但我想添加其他功能。 Resetting the filters with a button. 用按钮复位过滤器。 Also, at initialization, I want the select buttons to show blank or make it's value "". 另外,在初始化时,我希望选择按钮显示为空白或使其值为“”。
I know there's a way with this but I cannot come up with how. 我知道这是有办法的,但我无法提出办法。
Here's my jsFiddle 这是我的jsFiddle
I've got a large amount of data so here's the js code: 我有大量数据,所以这是js代码:
$(document).ready(function() {
var table = $('#example').DataTable({
dom: 'lrtip',
"columnDefs": [
{
"targets": [ 1 ],
"visible": false,
"searchable": true
},
{
"targets": [ 4 ],
"visible": false,
"searchable": true
},
{
"targets": [ 7 ],
"visible": false,
"searchable": true
}
],
"scrollY": '40vh',
"scrollCollapse": true,
initComplete: function() {
this.api().columns([1]).every(function() {
var column = this;
//console.log(column); --> not needed
var select = $("#TSFltr");
column.data().unique().sort().each(function(d, j) {
select.append('<option value="' + d + '">' + d + '</option>')
});
});
this.api().columns([4]).every(function() {
var column = this;
//console.log(column); --> not needed
var select = $("#bodyFltr");
column.data().unique().sort().each(function(d, j) {
select.append('<option value="' + d + '">' + d + '</option>')
});
});
this.api().columns([7]).every(function() {
var column = this;
//console.log(column); --> not needed
var select = $("#CTFltr");
column.data().unique().sort().each(function(d, j) {
select.append('<option value="' + d + '">' + d + '</option>')
});
});
$("#TSFltr,#bodyFltr,#CTFltr").material_select();
},
});
$('#TSFltr').on('change', function() {
var search = [];
$.each($('#TSFltr option:selected'), function() {
search.push($(this).val());
});
search = search.join('|');
table.column(1).search(search, true, false).draw();
});
$('#bodyFltr').on('change', function() {
var search = [];
$.each($('#bodyFltr option:selected'), function() {
search.push($(this).val());
});
search = search.join('|');
table.column(4).search(search, true, false).draw();
});
$('#CTFltr').on('change', function() {
var search = [];
$.each($('#CTFltr option:selected'), function() {
search.push($(this).val());
});
search = search.join('|');
table.column(7).search(search, true, false).draw();
});
});
I see that you are populating the drop downs from the content of the datatable using append. 我看到您正在使用append从数据表的内容填充下拉列表。 Therefore, I would suggest that you create the select with a single entry like this: 因此,我建议您使用以下单个条目创建选择:
<option value="">--Select size(s)--</option>
Create a resent function and simply set the values to the blank entry you create, like this: 创建一个resent函数,只需将值设置为您创建的空白条目,如下所示:
$("#TSFltr").val("");
This should also fire the change event on the drop downs. 这也应该触发下拉菜单中的change事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.