DataTables

DataTables Examples

HTML

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css">
<div class="container">
  <div class="row filterRecords">
    <div class="input-group col-xs-6 col-sm-6 col-md-3 mb-3">
      <input type="search" class="form-control" id="searchName" placeholder="Search Name" type="text">
    </div>
    <div class="input-group col-sm-6 col-md-3 mb-3">
      <input type="search" class="form-control" id="searchLang" placeholder="Search Language" type="text">
    </div>
    <div class="input-group col-sm-6 col-md-3 mb-3">
      <input type="search" class="form-control start" id="yearMin" placeholder="Search Start Year" type="text">
    </div>
    <div class="input-group col-sm-6 col-md-3 mb-3">
      <input type="search" class="form-control end" id="yearMax" placeholder="Search End Year" type="text">
    </div>
  </div>
  <div class="row justify-content-md-center">
    <div class="col-md-auto">
      <button class="btn btn-sm btn-default" id="resetFilter" type="button">Reset Filters</button>
      <hr>
    </div>
  </div>

  <p class="text-center">
    <table id="example" class="display" cellspacing="0" width="100%">
      <thead>
        <tr>
          <th>Name</th>
          <th>Position</th>
          <th>Office</th>
          <th>Age</th>
          <th>Start date</th>
          <th>Salary</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Tiger Nixon</td>
         ...

JavaScript

$(document).ready(function() {

  var table = $('#example').DataTable({
    dom: '<"wrapper"lipt>',
    paging: false,
    responsive: true,
    scrollY: "50vh",
    scrollCollapse: true,
    stateSave: true,
  });

  $('#searchName').keyup(function() {
    table
      .search($(this).val())
      .draw();
      save();
  });
  $('#searchLang').keyup(function() {
    table
      .search($(this).val())
      .draw();
  });
  $('#yearMin, #yearMax').on("keyup change", function() {
    table
      .draw();
  });
  $('#resetFilter').click(function() {
    $('input[type=text]').val('').change();
    table
      .search('')
      .columns().search('')
      .draw();
  });
  // reset button
  var $inputs = $(".filterRecords input");
  $('#resetFilter').click(function() {
    $inputs.val('').trigger('input');
  });
  
  function save(){
var text_to_save=document.getElementById('searchName').value;
localStorage.setItem("text", text_to_save); // save the item
}


function retrieve(){
var text=localStorage.getItem("text"); // retrieve
document.getElementById('searchName').innerHTML = text; // display
}
  
});