Create Pagination, Sorting, Filter With HTML Table Using DataTable.JS

Introduction 

In this blog, we will learn how to use DataTable.js to create a grid with pagination, sorting, filter etc.,using jQuery. Most developers create a simple HTML table and write huge amounts of code to make a grid with sorting, paging, etc. features .

If you are using jQuery DataTable.js, you don't need to write a huge amount of code, as you need to just write 3-4 lines of simple code.

You can find more information about DataTable.js from the link here.
 
DataTable.js can be used with ASP.NET, Java, MVC, PHP, etc.. 
 
Features of DataTable.js

There are many features and advantage of Datatable.js. You can use it anywhere, using simple jQuery. If you are using DataTable.js, you can apply these features, which are given below.
  • Inline Sorting.
  • Sorting on each column.
  • Pagination.
  • Filter.
  • Filter on each column.
  • Global Search.
  • Stylist Look .
  • Dynamic column binding.
  • Bind Json data.
  • Language Independent.
Basic requirment to use DataTable.js

If you want to use DataTable.js, first, you have to use the library, which is given below, else it will through an error.

CSS 
  1. <link rel="Stylesheet" href=" " "text="" javascript""="" src=""https://code.jquery.com/jquery-1.12.3.js""> " target="_blank">https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" />  
jQuery 
  1. <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.js"></script>  
DataTable.Js 
  1. <script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>   
How to use DataTable.js 

If you have added all libraries, create HTML table, whose code is mentioned below.
  1. <table id="MyTable" class="display" cellspacing="0" width="100%">  
  2.         <thead>  
  3.             <tr>  
  4.                 <th>First Name</th>  
  5.                 <th>Last Name</th>  
  6.                 <th>Postion</th>  
  7.                 <th>Technologies</th>  
  8.                 <th>Company Name</th>  
  9.                 <th>Experience</th>  
  10.             </tr>  
  11.         </thead>  
  12.         <tfoot>  
  13.             <tr>  
  14.                 <th>Name</th>  
  15.                  
  16.             </tr>  
  17.         </tfoot>  
  18.         <tbody>  
  19.             <tr>  
  20.                 <td>Bikesh</td>  
  21.                 <td>Srivastava</td>  
  22.                 <td>Software Engg.</td>  
  23.                 <td>Asp.net</td>  
  24.                 <td>Hytech</td>  
  25.                 <td>4</td>  
  26.             </tr>  
  27.             <tr>  
  28.                 <td>Navdeep</td>  
  29.                 <td>Kumar</td>  
  30.                 <td>Sr.Software Engg.</td>  
  31.                 <td>Asp.net</td>  
  32.                 <td>Hytech</td>  
  33.                 <td>8</td>  
  34.             </tr>  
  35.             <tr>  
  36.                 <td>Sujata</td>  
  37.                 <td>Sinha</td>  
  38.                 <td>Software Engg.</td>  
  39.                 <td>Asp.net</td>  
  40.                 <td>Hytech</td>  
  41.                 <td>2</td>  
  42.             </tr>  
  43.             <tr>  
  44.                 <td>Panakj</td>  
  45.                 <td>Bhanadari</td>  
  46.                 <td>Software Engg.</td>  
  47.                 <td>Asp.net</td>  
  48.                 <td>Hytech</td>  
  49.                 <td>3</td>  
  50.             </tr>  
  51.             <tr>  
  52.                 <td>Harikant</td>  
  53.                 <td>Kumar</td>  
  54.                 <td>Web Designer</td>  
  55.                 <td>Asp.net</td>  
  56.                 <td>Hytech</td>  
  57.                 <td>4</td>  
  58.             </tr>  
  59.             <tr>  
  60.                 <td>Payal</td>  
  61.                 <td>Agrawal</td>  
  62.                 <td>Software Engg.</td>  
  63.                 <td>Salesforce</td>  
  64.                 <td>Hytech</td>  
  65.                 <td>1</td>  
  66.             </tr>  
  67.             <tr>  
  68.                 <td>Pritam</td>  
  69.                 <td>Shekhawat</td>  
  70.                 <td>Manager</td>  
  71.                 <td>Salesforce</td>  
  72.                 <td>Hytech</td>  
  73.                 <td>3</td>  
  74.             </tr>  
  75.             <tr>  
  76.                 <td>Saurabh</td>  
  77.                 <td>Kumar</td>  
  78.                 <td>Software Engg.</td>  
  79.                 <td>Asp.net</td>  
  80.                 <td>HytechPro</td>  
  81.                 <td>6</td>  
  82.             </tr>  
  83.             <tr>  
  84.                 <td>Vinod</td>  
  85.                 <td>Kumar</td>  
  86.                 <td>Software Engg.</td>  
  87.                 <td>Asp.net</td>  
  88.                 <td>HytechPro</td>  
  89.                 <td>6</td>  
  90.             </tr>  
  91.             <tr>  
  92.                 <<td>Manik</td>  
  93.                 <td>Bansal</td>  
  94.                 <td>Software Engg.</td>  
  95.                 <td>SharePoint</td>  
  96.                 <td>HytechPro</td>  
  97.                 <td>3</td>  
  98.             </tr>  
  99.             <tr>  
  100.                 <td>Brijesh</td>  
  101.                 <td>Srivastava</td>  
  102.                 <td>Asst.Manager</td>  
  103.                 <td>Pharma</td>  
  104.                 <td>Sun Pharama</td>  
  105.                 <td>6</td>  
  106.             </tr>  
  107.             <tr>  
  108.                 <td>Krishu</td>  
  109.                 <td>Srivastava</td>  
  110.                 <td>Software Engg.</td>  
  111.                 <td>Asp.net</td>  
  112.                 <td>Hytech</td>  
  113.                 <td>4</td>  
  114.             </tr>              
  115.         </tbody>  
  116.     </table>  
Afterwards, create a JS code, which looks as shown below.
  1. $(document).ready(function() {  
  2.     $('#MyTable').DataTable( {  
  3.         initComplete: function () {  
  4.             this.api().columns().every( function () {  
  5.                 var column = this;  
  6.                 var select = $('<select><option value=""></option></select>')  
  7.                     .appendTo( $(column.footer()).empty() )  
  8.                     .on( 'change'function () {  
  9.                         var val = $.fn.dataTable.util.escapeRegex(  
  10.                             $(this).val()  
  11.                         );  
  12.                 //to select and search from grid  
  13.                         column  
  14.                             .search( val ? '^'+val+'$' : ''truefalse )  
  15.                             .draw();  
  16.                     } );  
  17.    
  18.                 column.data().unique().sort().each( function ( d, j ) {  
  19.                     select.append( '<option value="'+d+'">'+d+'</option>' )  
  20.                 } );  
  21.             } );  
  22.         }  
  23.     } );  
  24. } );  
Now, you are ready to run an Application. I hope, you have done all the settings and code fine.You can see the output, as shown below.
 
 
Now, you can see and use all the features of DataTable.js. I hope you understood all the things. If you have any doubt, you can download the attached complete solution.