fnFilter() How to reset all filters without multiple requests.
fnFilter() How to reset all filters without multiple requests.
Good day,
I have the problem with fnFilter inside columns. When I filtering by many columns it is great, but when I need to reset filtering the only way is to make:
table.fnFilter("",1,false)
table.fnFilter("",2,false)
...
which causes a lot of requests to the server.
Can I clear all filters at once without multiple requests?
Thanks in advance.
I have the problem with fnFilter inside columns. When I filtering by many columns it is great, but when I need to reset filtering the only way is to make:
table.fnFilter("",1,false)
table.fnFilter("",2,false)
...
which causes a lot of requests to the server.
Can I clear all filters at once without multiple requests?
Thanks in advance.
This discussion has been closed.
Replies
Good question :-) The way to do this is to make use of the DataTables settings object (obtained from fnSettings()) and set the oSettings.aoPreSearchCols[ iColumn ].sSearch parameters (for each column you want to clear the search of) to be a blank string. If you look in fnFilter (line 1199) you will see how this ties in with the filter.
Once sSearch has been cleared for each column, you can then just call fnDraw to redraw the table and - presto, single request :-)
Hope this helps,
Allan
Thank you so much! You are wizard.
I am flying to try it.
[code]
function fnResetAllFilters() {
var oSettings = oTable.fnSettings();
for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
oSettings.aoPreSearchCols[ iCol ].sSearch = '';
}
oTable.fnDraw();
//console.log(oSettings);
}[/code]
I changed the code to accept the table as argument (I'm using multiple tables) and to also reset of the global filter:
[code]
function fnResetAllFilters(oTable) {
var oSettings = oTable.fnSettings();
for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
oSettings.aoPreSearchCols[ iCol ].sSearch = '';
}
oSettings.oPreviousSearch.sSearch = '';
oTable.fnDraw();
}
[/code]
[code]
$.fn.dataTableExt.oApi.fnResetAllFilters = function (oSettings, bDraw/*default true*/) {
for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
oSettings.aoPreSearchCols[ iCol ].sSearch = '';
}
oSettings.oPreviousSearch.sSearch = '';
if(typeof bDraw === 'undefined') bDraw = true;
if(bDraw) this.fnDraw();
}
[/code]
usage :
[code]
//case 1
//clean all filter and redraw
oDataTable.fnResetAllFilters();
//case 2
//clean all filter and set new filter and redraw
oDataTable.fnResetAllFilters(false); //reset all filter , do not redraw
oDataTable.fnFilter("newkeyword", 4); //new filter and draw
[/code]
Allan
For Example, I have a search result that I want to persist using bStateSave = true (which works fine) but after a while I need to clear the saved state. I would like to have a button "Reset Filters" which would then trigger this nice resetAll function but I have zero clue on how to do this.
Right now I have this which I copied from above:
(function($) {
$.fn.dataTableExt.oApi.fnResetAllFilters = function (oSettings, bDraw/*default true*/) {
for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
oSettings.aoPreSearchCols[ iCol ].sSearch = '';
}
oSettings.oPreviousSearch.sSearch = '';
if(typeof bDraw === 'undefined') bDraw = true;
if(bDraw) this.fnDraw();
}
}(jQuery));
Do I have the above function placed correctly? Where do I put this "oDataTable.fnResetAllFilters();"?
[code]
var oTables = $('.dataTable').dataTable();
oTables.fnResetAllFilters();
[/code]
Allan
Allan
Hope this helps
[code]
oTable.fnFilter('',1); //if you were filtering the entries of second column
oTable.fnFilter('');//global filtering which will retrieve the whole datatable
[/code]