JQuery DataTables - 删除 fnFilter 并显示所有结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8423035/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:31:36  来源:igfitidea点击:

JQuery DataTables - Remove fnFilter and display all results

jqueryfilterdatatables

提问by JimmyJammed

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.

我目前有一个数据表,每个记录都有一个按钮,单击该按钮时会显示该帐户的其他信息。发生这种情况时,我调用 fnFilter() 来过滤该特定行,以便不显示其他行,并且用户知道我显示的子信息是针对该特定帐户的。我想要做的是,当用户在搜索工具栏中单击返回时,它会隐藏我显示的子信息,然后清除过滤器并显示所有可用的原始记录。一切正常,除了过滤器没有被清除,所以仍然只显示最初选择的行。

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!

不知道我错过了什么。我已经尝试了从使用 fnFilter('') 到 fnDraw() 到 fnReloadAjax() 的所有方法。这些(或任何组合)似乎都不起作用!

UPDATEI 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.

更新我似乎已经隔离了这个问题。如果我从 fnFilter(accountid,7) 中删除列 #,使用 fnFilter('') 会重新显示所有记录。但是,我确实需要按该特定列进行过滤,因为它是唯一包含每条记录的唯一值的列。有任何想法吗?我确实尝试使用 fnFilter('',null) 但没有成功。

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();
});

回答by JimmyJammed

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('');

回答by Dragan Mihajlovi?

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

使用 Datatables > 1.10 清除所有过滤器很简单:

oTable.search( '' ).columns().search( '' ).draw();

回答by Alfred Severo

on Datatables v1.10

在数据表 v1.10

$('.dataTables_filter input[type=search]').val(''); 

do the trick (clear the search box)

解决问题(清除搜索框)

回答by Daniel F

For me this plugin worked great:

对我来说,这个插件效果很好:

https://datatables.net/plug-ins/api/fnFilterClear

https://datatables.net/plug-ins/api/fnFilterClear

回答by raama ragavan

"sPaginationType" : "bootstrap",
"iDisplayLength": 25,
"aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
"bStateSave" : false,

if you want to save the search results then mention

如果您想保存搜索结果,请提及

"sPaginationType" : "bootstrap",
"iDisplayLength": 25,
"aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
"bStateSave" : true,