javascript jquery 数据表:如何清除列搜索过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28189045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery datatables: how to clear column search filter
提问by Finglish
I am using jquery datatablesto make my table searchable. I have a dropdown that filters a gender column:
我使用jQuery的数据表,使我的表进行搜索。我有一个过滤性别列的下拉列表:
$("#genderDrop").on("change", function(e) {
var gender = $(this).val();
formTable.column(2).search(gender).draw();
});
this works fine, but now I want to be able to remove the filter when the user selects "all" from the dropdown. Here is my attempt:
这工作正常,但现在我希望能够在用户从下拉列表中选择“全部”时删除过滤器。这是我的尝试:
$("#genderDrop").on("change", function(e) {
var gender = $(this).val();
if (gender != "all") {
formTable.column(2).search(gender).draw();
} else {
formTable.column(2).search("").draw();
}
});
Instead of removing the filter this just searches for an empty string, but I can't work out how to change this so it removes the filter. I also tried:
这不是删除过滤器,而是搜索一个空字符串,但我不知道如何更改它,因此它删除了过滤器。我也试过:
formTable.column(2).search("*").draw();
and
和
formTable.column(2).search().draw();
but without any success.
但没有任何成功。
回答by Paulo Segundo
You can use the option All from gender select with empty value:
您可以使用带有空值的所有来自性别选择的选项:
<option value="">All</option>
them your code will work:
他们您的代码将起作用:
$("#genderDrop").on("change", function(e) {
var gender = $(this).val();
formTable.column(2).search(gender).draw();
});
Using DataTables example as base: http://jsfiddle.net/PauloSegundo/g15xakh5/
使用 DataTables 示例作为基础:http: //jsfiddle.net/PauloSegundo/g15xakh5/
回答by Rajesh B
you can try the below:
您可以尝试以下方法:
function fnResetAllFilters() {
var oSettings = oTable.fnSettings();
for (iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
oSettings.aoPreSearchCols[iCol].sSearch = '';
}
oTable.fnDraw();
}
To remove all filters and global filters, please refer to below link: https://datatables.net/plug-ins/api/fnFilterClear
要删除所有过滤器和全局过滤器,请参考以下链接:https: //datatables.net/plug-ins/api/fnFilterClear
回答by Javier Contreras
Try this way:
试试这个方法:
var table = $('#tableHere').DataTable().destroy();
table.state.clear();