Remove search filter on Datatable
Asked Answered
A

3

32

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

Aldwin answered 10/12, 2014 at 13:26 Comment(0)
V
65

Maybe you are looking at something like this: http://www.datatables.net/plug-ins/api/fnFilterClear
You could clear the search in a very simple way:

var table = $('#example').DataTable();
table
 .search( '' )
 .columns().search( '' )
 .draw();
Venettavenezia answered 10/12, 2014 at 13:38 Comment(1)
Yes, but how would that be triggered?Trochee
K
7

More easy

var table = $('#example').DataTable();
table
.search("").draw(); 
Kessinger answered 19/9, 2018 at 18:49 Comment(0)
J
0

If you're looking to check for an existing search filter being applied, then clear out a specific filter only, you can accomplish it like so:

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

Note: I'm using the 'stateSave': true option on the Datatable to keep state between page requests.

Jerad answered 31/1, 2020 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.