JQuery DataTables - Remove fnFilter and display all results

39,241

Solution 1

Ah I seemed to have figured it out. Have to clear out the filter on that specific column AND the global filter:

oTable.fnFilter('',7);
oTable.fnFilter('');

Solution 2

It is simple to clear all filters using Datatables > 1.10:

oTable.search( '' ).columns().search( '' ).draw();
Share:
39,241
JimmyJammed
Author by

JimmyJammed

Software Engineer Experienced In: iOS / Objective-C / Swift PHP Javascript / jQuery mySQL / SQL HTML / CSS Graphic Design

Updated on October 31, 2020

Comments

  • JimmyJammed
    JimmyJammed over 3 years

    I current have a datatable that has a button for each record that when clicked displays other information for that account. When this happens, I call fnFilter() to filter that specific row so that no other ones are displayed and the user knows that the sub-information I display is for that specific account. What I would like to do, is when a user clicks back in the search toolbar, it hides the sub-information I displayed, then clears the filter and shows all the original records available. Everything works fine, except that the filter doesnt get cleared so only the originally selected row is still displayed.

    Not sure what I am missing. I have tried everything from using fnFilter(''), to fnDraw(), to fnReloadAjax(). None of these (or any combination) seem to work!

    UPDATE I seemed to have isolated the problem. If I remove the column # from the fnFilter(accountid,7), using fnFilter('') does re-display all records. However, I really need to filter by that specific column as it is the only column that contains unique values for each record. Any ideas? I did try using fnFilter('',null) but no success.

    Here is my code:

    var oTable = $('.mypbhs_accounts').dataTable({
            "bProcessing": true,
            "sAjaxSource": 'sql/mypbhs_accounts.php',      
            "aaSorting": [[1, "asc" ]],
            "bJQueryUI": true,
            "sPaginationType": "full_numbers",
            //"bStateSave": true, //Use a cookie to save current display of items
            "aoColumns": [
                {"asSorting": [  ], "sClass":"center"},
                null,
                null,
                null,
                null,
                null,
                null,
                { "bSearchable": true, "bVisible": false },       
                { "bSearchable": true, "bVisible": false }       
            ],
            "bScrollCollapse": true,
            "sScrollX": "100%",
             "fnInitComplete": function() {
                    oTable.fnAdjustColumnSizing();
             }
        });
    /*** CLEAR CURRENT ACCOUNT INFO ***/
    $(document).on('click','.mypbhs_content .dataTables_filter',function(){ //THIS IS CALLED WHEN USER CLICKS INTO THE SEARCH BAR
        $('.mypbhs_content .dataTables_filter :input').val(''); //CLEAR CURRENT VALUE IN THE SEARCH BAR
        oTable.fnFilter('');
        //oTable.fnDraw();
        //oTable.fnReloadAjax();
        $('.mypbhs_truform_info').empty(); //REMOVE SUB-INFORMATION SO IT DOESNT GET ASSOCIATED WITH WRONG ACCOUNT
        $('.control_bar').children('ul.mypbhs_account_controls').empty();
    });