jQuery Datatables Implementation

jQuery Datatables

The jQuery Plugin Datatable will help you to keep those huge database results into a better and organize table. I have spoke of the jQuery Datatables plugin into this entry, so I will no use much time in the introduction of the plugin. I will focus and how you can get you database results into that Datatable.

For this Example I will be using a database, the employee table that I have been using in previous tutorials, so if you don’t remember the file structure of the table I present it again.

This is the structure of the table (if you download the code you will get a full explanation of the table):

Once you create the table fill it with the require information, I suggest you to add many records so that you can use the Datatable Pagination. As I mentioned before if you download the code you will have all the information in a sql script.

DOWNLOADING JQUERY DATATABLES

You can take the code from these two sources:

1) Download the code from the offcial page or

Download Datatables

2) At the end of the post you can download all the code with the Database included. Also all the inserts of the table employee are included.

THE MARKUP

We have to setup a place where our information is going to appear so basically we only need to make our table where the data will be display. This is the basic html that we need.

ADDING THE RESOURCES

Now that you have setup the code for the table we have to add the resources previously downloaded. To complete this, add the following code in your header or before the closing body tag.

Notice that I have added an extra script named app.js, this is where all our Javascript will be written.

RUNNING JQUERY DATATABLES.

Applying the jQuery datatables plugin is very easy, just like any other jQuery plugin you have to initialize it. Remember that in the HTML table we setup the class id resultTable, so we are going to use that as our selector.

app.js

RESULT

Until this point with that small portion of code you should be able to see the jQuery datatable in action, of course this is where things get interesting and complex and therefore I will make more tutorials to target the must common situation.

Datatables in Action
Datatables in Action

MANY RECORDS

In the beginning of this post I mentioned that using jQuery datatables is for the purpose of controlling many records in our application, if we don’t do that then we will end up with a table with huge scrolling. Add many records into the employee table using you database editor or manually using the MySQL console, on this point section we are not going to write the records into hour html table, instead we are going to retrieve all this information from our database.

I have created some other tutoriales to fetch information with Ajax so you could take a look at Ajax usage to search a Record with PHP, MySQL and jQuery or this other tutorial Find a Record with Ajax and jQuery UI Autocomplete, PHP, MySQL and Json – Part 1.

This is the code to retrieve the information with jQuery Ajax, the server side code I leave it to you for the implementation or you can purchase the code that includes the design that you see on the images as well as all the complete database structure and with all the records.

RESULT

004-datatables-database-ex

The table above has many records and is using the jQuery Datatables in Action, there are 5 elements that are given by default:

  1. Dropdown to select the number of records to be displayed.
  2. You can search information on the table itselft by using this input area.
  3. You can sort the records
  4. A Legend that displays the number of records been displayed as well as the total.
  5. A Paginator where you can jump to any page that you desire.

Implementing the plugin Datatables is not a complex thing, customizing the plugin make the things interesting. On future post I will be showing how to add or remove the elements of the Datatables or also adding filters/custom input areas on the headers of the table so that you can search by column and not globally, also I will be showing who to load information on the Datatables dynamically using Ajax, this is to avoid a heavy request to the server when there are many records on the database.

FINAL RAW CODE

DOWNLOAD THE FULL CODE

You can always purchase the code, this will support the developer and also pay for the hosting and domain services. This is what you get with the purchase:

  • Current style that is shown on the images.
  • Boostrap HTML code for the examples.
  • Server side code to retrieve the information.
  • Database file to avoid the insertions of records.
  • Full code commented.
  • Help to implement THIS code.