fnFilter() How to reset all filters without multiple requests.

fnFilter() How to reset all filters without multiple requests.

JuryJury Posts: 5Questions: 0Answers: 0
edited December 2009 in General
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.

Replies

  • JuryJury Posts: 5Questions: 0Answers: 0
    Please, help anybody. May be my question so stupid, I know, but I can't solve it =(
  • JuryJury Posts: 5Questions: 0Answers: 0
    Anybody...
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hi Jury,

    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
  • JuryJury Posts: 5Questions: 0Answers: 0
    Hi Allan,

    Thank you so much! You are wizard.
    I am flying to try it.
  • mindsharemindshare Posts: 6Questions: 0Answers: 0
    edited February 2010
    Would this type function be the best way to handle this? I just want to make sure I'm using fnDraw() appropriately here...

    [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]
  • JuryJury Posts: 5Questions: 0Answers: 0
    Yes, it seems to be right decision.
  • Martin_SMartin_S Posts: 8Questions: 0Answers: 0
    edited September 2010
    Thanks for this code. My first try was calling 'fnFilter( '', index)' in a loop, which is not very efficient.

    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]
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    This is good stuff. My performance was being crushed as well until I found this approach. Maybe there should be a function in datatables, or post this to the examples or api pages.
  • dulao5dulao5 Posts: 1Questions: 0Answers: 0
    edited August 2011
    I adjust @Martin_S 's source code , add fnResetAllFilters function to dataTableExt.oApi ; and add a 'bDraw' bool flag .

    [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]
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    There is a plug-in API method for this already: http://datatables.net/plug-ins/api#fnFilterClear . Doesn't have some of the nice features added by dulao5 above tho - nice one :-)

    Allan
  • afindley.cbizafindley.cbiz Posts: 3Questions: 0Answers: 0
    So how would one access "oDataTable.fnResetAllFilters();"

    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();"?
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    If you've put the plug-in code in ( http://datatables.net/release-datatables/examples/plug-ins/plugin_api.html ), all you need to do is:

    [code]
    var oTables = $('.dataTable').dataTable();
    oTables.fnResetAllFilters();
    [/code]

    Allan
  • AllanSPalmerAllanSPalmer Posts: 3Questions: 0Answers: 0
    So I have a rather unique issue with clearing the filter. We are controlling the rendering of the table columns prior to the datatable initialisation. Columns can be turned on or off and the page is reloaded. We're also enabling column level filtering, but when the page is reloaded after columns are not rendered (i.e. hidden), the column filters get mis-aligned (so, the search in one column is actually being applied to the adjacent column's data). I've tried the solution that Martin_S has listed above and even threw in a fnClearTable(true); for good measure, but still no luck. Any suggestions?
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Sounds like you probably want to use this plug-in to convert from visible column index to column data index: http://datatables.net/plug-ins/api#fnVisibleToColumnIndex .

    Allan
  • vimalmahivimalmahi Posts: 1Questions: 0Answers: 0
    edited July 2012
    Try this out..First we need to take away the filter for the column we specified.. Then we have to set a global one with blank.

    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]
This discussion has been closed.