Easy search filters for Datatables

Custom search filters for Datatables

by dying_sakura

HTML

<link rel="stylesheet" href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css">
<script src="//cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<form class='filter-form'>
  <h3>Filters</h3>
  <div>
    <label>Filter :</label>
    <input type='text' value='' class='filter'>
  </div>
</form>
<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Reject</th>
      <th>Approvd</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Reject</th>
      <th>Approvd</th>
      <th>Salary</th>
    </tr>
  </tfoot>
  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>Pending</td>
      <td>Pending</td>
      <td>$320,800</td>
    </tr>
 

  </tbody>
</table>

CSS

.filter-form {
  margin-bottom: 30px;
}

.filter-form div {
  margin-bottom: 10px;
}

JavaScript

$(document).ready(function() {

  // DataTable
  var dtable = $('#example').DataTable();

  $('.filter').on('keyup change', function() {
    dtable
      .column(4).search(this.value)
     
      .draw();
  });

  $(".dataTables_filter input").on('keyup change', function() {
    //clear column search values
    dtable.columns().search('');
    //clear input values
    $('.filter').val('');
  });

});