jQuery 如何重置数据表中的过滤器

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

how to reset the filters in a datatable

jquerydatatables

提问by Onera

I am unable to clear the filters using the fnFilter().

我无法使用 fnFilter() 清除过滤器。

Here is my HTML code :

这是我的 HTML 代码:

<table id="todays _table>

               <thead>
                 <tr id="todays_search_input_feilds">
                     <td class="input_filter">
                        <input type="text" id="type" class="form-control search_events">
                    </td>
                    <td class="input_filter">
                        <input type="text" id="vendor " class="form-control search_events">
                    </td>
                </tr>
            </thead>
    </table> 

And Here is my JQuery Code

这是我的 JQuery 代码

$('#todays_table').dataTable().fnFilter('');
    oTable.fnClearTable();

I tried clearing using the following approach:

我尝试使用以下方法清除:

$('input[class="form-control search_events"]').val('');

But the problem with this is that, it is clearing the value but the data is not loading in the dataTable. It is loading only after i click on any of the filters

但问题在于,它正在清除值但数据未加载到数据表中。它仅在我单击任何过滤器后加载

回答by Gyrocode.com

To reset custom filters

重置自定义过滤器

If you're using custom filtering function as in this example, you need to clear controls involved before filtering and redrawing the table.

如果你在这个例子中使用自定义过滤功能,你需要在过滤和重绘表格之前清除涉及的控件。

$('input.search_events').val('');
$('#todays_table').dataTable().fnDraw();

To reset global search

重置全局搜索

  • jQuery DataTables 1.9+

    Call fnFilter()API method with empty string as a first argument to reset the global search and redraw the table.

    For example:

    $('#example').dataTable().fnFilter('');
    
  • jQuery DataTables 1.10

    Call search()API method with empty string as a first argument followed by call to draw()API method to reset the global search and redraw the table.

    For example:

    $('#example').DataTable().search('').draw();
    
  • jQuery 数据表 1.9+

    fnFilter()以空字符串作为第一个参数调用API 方法以重置全局搜索并重新绘制表格。

    例如:

    $('#example').dataTable().fnFilter('');
    
  • jQuery 数据表 1.10

    调用search()API方法以空字符串作为第一个参数,然后调用draw()API方法来重置全局搜索和重绘表。

    例如:

    $('#example').DataTable().search('').draw();
    

回答by Aline Matos

For those who's performing a search with columnand regex:

对于那些使用columnregex执行搜索的人:

$('#example').DataTable().column('').search('').draw();

回答by kalibrain

DataTables 1.10+ new API can achieve the same effect without the requirement for plug-ins using the following chaining:

DataTables 1.10+ 新 API 无需插件即可实现相同效果,使用以下链接:

var table = $('#example').DataTable(); 
table.search('').columns().search('').draw();

If you are using earlier versions of DataTables, you need to include plugin library called fnFilterClearand call:

如果您使用的是早期版本的 DataTables,则需要包含名为fnFilterClear 的插件库并调用:

var table = $('#example').DataTable(); 
table.fnFilterClear();

Please see this DataTables API pagefor more information and the plugin that needs to be used for earlier versions of DataTable.

请参阅此DataTables API 页面以获取更多信息以及需要用于早期版本 DataTable 的插件。

回答by akshika gupta

 <script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.10.9/api/fnFilterClear.js"></script>

  $.extend( $.fn.dataTable.defaults, {
 fnInitComplete: function(oSettings, json) {
   var btnClear = $('#clear_all');
  btnClear.appendTo($('#' + oSettings.sTableId).parents('.dataTables_wrapper').find('.dataTables_filter'));

  $('#clear_all').click(function () {
   $('#' + oSettings.sTableId).dataTable().fnFilterClear();
   $("input.column_filter").val('');

       $.fn.dataTable.ext.search.pop();
  });

 }

});

回答by Chandana

I am using DataTables 1.10.15 and it is working for as below.

我正在使用 DataTables 1.10.15,它的工作方式如下。

        var oTable = $('#example').DataTable();
        $.fn.dataTableExt.afnFiltering.length = 0;
        oTable.draw();

回答by Hosam alzagh

DataTables 1.10+ new API

数据表 1.10+ 新 API

var D_T = $(selector).DataTable();
     D_T.
         search('').
         columns().search('').visible( true, true ).order('asc' ).
         colReorder.reset().
         page.len(10).
         state.clear().
         draw() ;