Skip to main content

Adding extra columns in jQuery Datatable


JQuery Datatables is an awesome and easy to use plugin for displaying data in a table from almost any source. It is very flexible and easy to use. I however came across a little problem of adding extra columns in a dynamic table. I googled and still could not find a very suitable or easy to implement solution so I had to improvise with the technique below.
In order to try this, you would have to get a copy of the Datatables plugin. Get your hands dirty quickly by trying out this simple setup found on the datatables website which pulls data from a mysql table using php. If you followed the example link, you would end up touching about 2 main files.

1) server_processing.php located in the examples_support folder
2) server_side.html located in -> examples/server_side/
If you were successful, you should see something close to the image below when you run it in your web server:
Initial setup of DataTable
Now, to the fun part of adding an extra column. In this example, we would include an extra column which is a link to edit that particular row using the row's id. We would do this without touching the javascript. It is just simple html and php. 
Step 1 Open server_processing.php located in the examples_support folder. Copy line 9 which has the $aColumns array variable:
$aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade');
 On line 146 paste and modify the array to reflect what is below. I just had to include the 'id' field. 
 $aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade','id' );
Step 2 - Modify the queryThis is the shortest step. You need to include the 'id' column to the query. Modify the query on line 116  to 122 and include the 'id' column: 
Step 3 - Add it to the json echoBetween line 151 and 168, you should find a while loop which actually generates the output for the datatable:
Line 151 to 168
We would include another 'elseif' condition which would check if the current row it is iterating is the id field, please note that you can always change this condition. If it is the 'id' field it would display a link in that column: 
Lines 151 - 174
Step 4 - Add another column to the display (html)Open server_side.html located in -> examples/server_side/. Because we had to introduce another column in the array used to fetch data in the server side, we would have to do the same here in the html or whichever file you use to display your data. 
Simply add another column here in the table and that should solve the problem. 
If you were able to go through these steps you should see something like this: 
Demo view
Update: I have uploaded the example files I used to http://dl.dropbox.com/u/255987/tumblr/datatable/example_files.zip
You can download it to have a look. 

Comments

Popular posts from this blog

EC isn't prepared to take technology to the next level in Elections

In Election 2012, Ghana's EC introduces a new process to verify voters before they are allowed to cast their vote. This was in the form of biometric verification, a good idea but poorly not so well implemented.

Be a part of the experience, and make sure your Polling Station is represented on Google Maps!

Excited to see the number of polling stations that have been mapped by Ghanaian voters. It is validation of the belief in the internet’s role in helping to ensure that the 2012 elections will be the best ever held in Ghana.