25

I have applied a button in my DataTable, which on click, filters the data table, to just show the clicked row.

table initialization is:

       var oDatatable = $("#tblDataTable").DataTable({
            dom: '<"top"CRTl><"clear">rt<"bottom"ip><"clear">',
            columns: [
                                   { data: 'Message' },
                { data: 'MessageId' },
                { data: null, "defaultContent": "<button id=\"tblRowData\">Click</button>"}
            ],

            "columnDefs": [
             { "visible": false, "targets": 0 }
            ]
           });

and my click event is:

    $('#tblDataTable tbody').on('click', 'button', function (event) {
    var data = oDataTable.row($(this).parents('tr')).data();
    oDataTable
     .columns(8)
     .search(data['MessageId'])
     .draw();

This all work perfectly fine, but now I want to reset the filters, when any other action on the page is carried out. For instance, changing a datetime picker.

How can I check if the datatable has a seach filter applied, and remove it (i.e. resetting the table back, prior to the click event).

4

3 回答 3

55

也许您正在查看类似这样的内容:http
://www.datatables.net/plug-ins/api/fnFilterClear 您可以以非常简单的方式清除搜索:

var table = $('#example').DataTable();
table
 .search( '' )
 .columns().search( '' )
 .draw();
于 2014-12-10T13:38:44.963 回答
5

更容易

var table = $('#example').DataTable();
table
.search("").draw(); 
于 2018-09-19T18:49:32.330 回答
0

如果您要检查正在应用的现有搜索过滤器,然后只清除特定过滤器,您可以像这样完成它:

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

    // The index of the column being searched
    var colIdx = 3;

    // Retrieve the current stored state of the table
    var tableState = table.state.loaded();

    // Retrieve the stored search value of the column
    var filterForColIdx = tableState.columns[colIdx].search.search;

    if ( '' !== filterForColIdx ) {
      // Clear the search term and re-draw
      table
       .columns( colIdx )
       .search( '' )
       .draw();
    }

注意:我使用'stateSave': trueDatatable 上的选项来保持页面请求之间的状态。

于 2020-01-31T17:48:43.600 回答