4

I am using datatables v1.10.11 and Jquery v 2.2.0

I have one table with two input search filters;

<input type="text" id="myInputTextField1" class="form-control"> <!--search one-->

<input type="text" id="myInputTextField2" class="form-control"> <!--search two-->

My datatable JS;

$(document).ready(function() {
    $('#example').dataTable({});
}); 

$('#myInputTextField1').keyup(function(){
  table.search($(this).val()).draw() ;
})

$('#myInputTextField2').keyup(function(){
  table.search($(this).val()).draw() ;
})

The search is working fine, no issues.

How would I incorporate a simple button that when clicked, will clear both input fields and reset the table to it's default state? For example;

<button type="button" id="test">Clear Filters</button>

<table id="example">
<thead>
  <tr>
    <th>COLUMN 1</th>
    <th>COLUMN 2</th>
    <th>COLUMN 3</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>ROW 1</td>
    <td>ROW 1</td>
    <td>ROW 1</td>
  </tr>
  <tr>
    <td>ROW 2</td>
    <td>ROW 2</td>
    <td>ROW 2</td>
  </tr>
  <tr>
    <td>ROW 3</td>
    <td>ROW 3</td>
    <td>ROW 3</td>
  </tr>  
</tbody>
</table>

Any help is appreciated.

2 Answers 2

7

To reset a search filter, just call search() again with an empty string. Similarly you can clear the value of an input by setting its value to an empty string. Try this:

$('#test').click(function() {
    $('#myInputTextField1, #myInputTextField2').val('');
    table.search('').draw(); //required after
});

Working example

Sign up to request clarification or add additional context in comments.

Comments

0
<button type="button" id="test" class="btn btn-primary">Clear Filters</button>

  $('#test').click(function () {
              $('input:text').val('');
              var datatableVariable = $('#tblBusinessDev').dataTable();
              datatableVariable.fnFilterClear();
              $('#tblBusinessDev tfoot input').val('').change();
          });


//filter code

jQuery.fn.dataTableExt.oApi.fnFilterClear = function (oSettings) {
              var i, iLen;

              /* Remove global filter */
              oSettings.oPreviousSearch.sSearch = "";

              /* Remove the text of the global filter in the input boxes */
              if (typeof oSettings.aanFeatures.f != 'undefined') {
                  var n = oSettings.aanFeatures.f;
                  for (i = 0, iLen = n.length ; i < iLen ; i++) {
                      $('input', n[i]).val('');
                  }
              }

              /* Remove the search text for the column filters - NOTE - if you have input boxes for these
               * filters, these will need to be reset
               */
              for (i = 0, iLen = oSettings.aoPreSearchCols.length ; i < iLen ; i++) {
                  oSettings.aoPreSearchCols[i].sSearch = "";
              }

              /* Redraw */
              oSettings.oApi._fnReDraw(oSettings);
          };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.