This post is first part of a series called Getting Started with Datatable 1.10 .
- DataTable demo (Server side) in Php,Mysql and Ajax
- jQuery Datatable with Custom Json format in Php, Mysql
- DataTable (Server side) Custom Column Search
- Datatable Scroller (Server Side)
- DataTable Responsive (Server side)
- Datatable Custom Loader
- Datatable Colvis (Column Visibility Customization)
- DataTable Table tools
- DataTable Custom Search By Tags Input (Server side)
- DataTable Search By Datepicker (Server side)
- Datatable Bulk Delete (Server Side)
- Datatable Page Resize (Server Side)
Grid view is a very important web component in modern web. Sorting, searching, pagination is not a easy job in HTML tables. So many grid view framework out there, DataTable.js is the most popular among them. It is open source, light weighted, highly flexible and customizable, features like AutoFill, inline editor, sticky header, responsive, Supports bootstrap, foundation. In version 1.10 DataTable has changed and improved over version 1.9. An entirely new API is available in DataTables 1.10.
In my blog I will try to explore datatable 1.10. So lets get started with most popular gridview framework in the planet.
In basic initialization datatable provides pagination, sorting, instant searching by loading whole data records at once. It can be a performance issue fetching large amount of data from server side. It will be better if you integrate server side pagination, searching and sorting, so we can break large amount data in chunk, So performance will increase significantly. Before proceed please take a look of the demo.
First i have created a index.php, where I have written html markup and basic initialization. created a table wth id “employee-grid” containing three column “Employee name”, “Salary”, “Age”.
<table id="employee-grid" cellpadding="0" cellspacing="0" border="0" class="display" width="100%"> <thead> <tr> <th>Employee name</th> <th>Salary</th> <th>Age</th> </tr> </thead> </table>
Now its time to initialize the datatable with your table “employee-grid” . By ajax api we can fetch json the data from employee-grid-data.php and datatble will automatically handle fetched data and append after “thead”.
Datatable send a request to “employee-grid-data.php” with so many parameters like sorting column index, order, search value, start and length/limit and draw number.
So what is draw ? sending request for any operation by datatable is called draw. it send a random number and recieve same same number as draw to ensure that the Ajax returns from server-side requests are drawn in sequence by DataTables.
In employee-grid-data.php you can see the parameter by print the Array $_REQUEST, like.
<?php echo "<pre>"; print_r($_REQUEST); echo "</pre>"; ?>
Array ( [draw] => 2 [columns] => Array (  => Array ( [data] => 0 [name] => [searchable] => true [orderable] => true [search] => Array ( [value] => [regex] => false ) )  => Array ( [data] => 1 [name] => [searchable] => true [orderable] => true [search] => Array ( [value] => [regex] => false ) )  => Array ( [data] => 2 [name] => [searchable] => true [orderable] => true [search] => Array ( [value] => [regex] => false ) ) ) [order] => Array (  => Array ( [column] => 0 [dir] => asc ) ) [start] => 0 [length] => 10 [search] => Array ( [value] => b [regex] => false ) )
Here I have created a columns array which contains database columns according to datatable column index. Because datatable sends only column index.
<?php $columns = array( // datatable column index => database column name 0 =>'employee_name', 1 => 'employee_salary', 2=> 'employee_age' ); ?>
Now we have to create json object which will contain .
- draw: we send same number which has been send by datatable at time of draw/request.
- recordsTotal: Total numbers of records in your table.
- recordsFiltered: Total numbers of filtered records after searching in your table. If you do not perform any search then recordsFiltered will be same as recordsTotal.
- data: Your fetched records data. You have to fetched the data as per start, length, search value, colomn name and sorting orders parameters. you can download dummy database table from here
$json_data = array( "draw" => intval( $_REQUEST['draw'] ), "recordsTotal" => intval( $totaldata ), "recordsFiltered" => intval( $totalfiltered ), "data" => $data ); echo json_encode($json_data);
You can download full code from below or improve my code by forking at Github.