Introduction
In this blog we will learn about "How to use DataTable.js to create a grid with pagination,sorting,filter etc..using Jquery.". Most of developer creating a simple html table and write huge amount of code to make a grid with sorting,paging,etc..features .
Using Jquery DataTable.js you don't need to write huge amount of code,just you need to write simple 3-4 line of code.
You can find more information about DataTable.js from this link ,click here
DataTable.js can be use with Asp.net,Java,MVC,PHP,etc..
Features of DataTable.js
There are many features and advantage of Datatable.js .you can use 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 ,then firstly you have to use these Library which are given below.otherwise it will through error.
CSS
<link rel="Stylesheet" href=" " target="_blank">https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" />
Jquery
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.js"></script>
DataTable.Js
<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 library ,Create a HTML Table Like this.see code below.
- <table id="MyTable" class="display" cellspacing="0" width="100%">
- <thead>
- <tr>
- <th>First Name</th>
- <th>Last Name</th>
- <th>Postion</th>
- <th>Technologies</th>
- <th>Company Name</th>
- <th>Experience</th>
- </tr>
- </thead>
- <tfoot>
- <tr>
- <th>Name</th>
- </tr>
- </tfoot>
- <tbody>
- <tr>
- <td>Bikesh</td>
- <td>Srivastava</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>4</td>
- </tr>
- <tr>
- <td>Navdeep</td>
- <td>Kumar</td>
- <td>Sr.Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>8</td>
- </tr>
- <tr>
- <td>Sujata</td>
- <td>Sinha</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>2</td>
- </tr>
- <tr>
- <td>Panakj</td>
- <td>Bhanadari</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>3</td>
- </tr>
- <tr>
- <td>Harikant</td>
- <td>Kumar</td>
- <td>Web Designer</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>4</td>
- </tr>
- <tr>
- <td>Payal</td>
- <td>Agrawal</td>
- <td>Software Engg.</td>
- <td>Salesforce</td>
- <td>Hytech</td>
- <td>1</td>
- </tr>
- <tr>
- <td>Pritam</td>
- <td>Shekhawat</td>
- <td>Manager</td>
- <td>Salesforce</td>
- <td>Hytech</td>
- <td>3</td>
- </tr>
- <tr>
- <td>Saurabh</td>
- <td>Kumar</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>HytechPro</td>
- <td>6</td>
- </tr>
- <tr>
- <td>Vinod</td>
- <td>Kumar</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>HytechPro</td>
- <td>6</td>
- </tr>
- <tr>
- <<td>Manik</td>
- <td>Bansal</td>
- <td>Software Engg.</td>
- <td>SharePoint</td>
- <td>HytechPro</td>
- <td>3</td>
- </tr>
- <tr>
- <td>Brijesh</td>
- <td>Srivastava</td>
- <td>Asst.Manager</td>
- <td>Pharma</td>
- <td>Sun Pharama</td>
- <td>6</td>
- </tr>
- <tr>
- <td>Krishu</td>
- <td>Srivastava</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>4</td>
- </tr>
- </tbody>
- </table>
- $(document).ready(function() {
- $('#MyTable').DataTable( {
- initComplete: function () {
- this.api().columns().every( function () {
- var column = this;
- var select = $('<select><option value=""></option></select>')
- .appendTo( $(column.footer()).empty() )
- .on( 'change', function () {
- var val = $.fn.dataTable.util.escapeRegex(
- $(this).val()
- );
- //to select and search from grid
- column
- .search( val ? '^'+val+'$' : '', true, false )
- .draw();
- } );
- column.data().unique().sort().each( function ( d, j ) {
- select.append( '<option value="'+d+'">'+d+'</option>' )
- } );
- } );
- }
- } );
- } );
Now you can see and use all features of DataTable.js .
I hope you understand all thing ,if you can see complete code below.
- <html>
- <head>
- <link rel="Stylesheet" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" />
- <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.js"></script>
- <script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
- <script>
- $(document).ready(function() {
- $('#MyTable').DataTable( {
- initComplete: function () {
- this.api().columns().every( function () {
- var column = this;
- var select = $('<select><option value=""></option></select>')
- .appendTo( $(column.footer()).empty() )
- .on( 'change', function () {
- var val = $.fn.dataTable.util.escapeRegex(
- $(this).val()
- );
- //to select and search from grid
- column
- .search( val ? '^'+val+'$' : '', true, false )
- .draw();
- } );
- column.data().unique().sort().each( function ( d, j ) {
- select.append( '<option value="'+d+'">'+d+'</option>' )
- } );
- } );
- }
- } );
- } );
- </script>
- </head>
- <body>
- <table id="MyTable" class="display" cellspacing="0" width="100%">
- <thead>
- <tr>
- <th>First Name</th>
- <th>Last Name</th>
- <th>Postion</th>
- <th>Technologies</th>
- <th>Company Name</th>
- <th>Experience</th>
- </tr>
- </thead>
- <tfoot>
- <tr>
- <th>Name</th>
- </tr>
- </tfoot>
- <tbody>
- <tr>
- <td>Bikesh</td>
- <td>Srivastava</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>4</td>
- </tr>
- <tr>
- <td>Navdeep</td>
- <td>Kumar</td>
- <td>Sr.Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>8</td>
- </tr>
- <tr>
- <td>Sujata</td>
- <td>Sinha</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>2</td>
- </tr>
- <tr>
- <td>Panakj</td>
- <td>Bhanadari</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>3</td>
- </tr>
- <tr>
- <td>Harikant</td>
- <td>Kumar</td>
- <td>Web Designer</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>4</td>
- </tr>
- <tr>
- <td>Payal</td>
- <td>Agrawal</td>
- <td>Software Engg.</td>
- <td>Salesforce</td>
- <td>Hytech</td>
- <td>1</td>
- </tr>
- <tr>
- <td>Pritam</td>
- <td>Shekhawat</td>
- <td>Manager</td>
- <td>Salesforce</td>
- <td>Hytech</td>
- <td>3</td>
- </tr>
- <tr>
- <td>Saurabh</td>
- <td>Kumar</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>HytechPro</td>
- <td>6</td>
- </tr>
- <tr>
- <td>Vinod</td>
- <td>Kumar</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>HytechPro</td>
- <td>6</td>
- </tr>
- <tr>
- <<td>Manik</td>
- <td>Bansal</td>
- <td>Software Engg.</td>
- <td>SharePoint</td>
- <td>HytechPro</td>
- <td>3</td>
- </tr>
- <tr>
- <td>Brijesh</td>
- <td>Srivastava</td>
- <td>Asst.Manager</td>
- <td>Pharma</td>
- <td>Sun Pharama</td>
- <td>6</td>
- </tr>
- <tr>
- <td>Krishu</td>
- <td>Srivastava</td>
- <td>Software Engg.</td>
- <td>Asp.net</td>
- <td>Hytech</td>
- <td>4</td>
- </tr>
- </tbody>
- </table>
- </body>
- </html>
Bikesh Srivastava
JQUERY