jQuery Tables with sort and search

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In this tutorial, we will use DataTables.net (a jQuery plugin) to make a table with sort and search capabilities.  Demo here.

We start with a normal HTML table.  But make sure to use the thead and tbody tags as shown below because DataTables will look for these tags to do its magic.

example table

example table

This is an example table listing the underscore.js collection functions.  With plain HTML table, it looks like this …

plain HTML table

plain HTML table

Not looking too great. Huh?

Now we add in DataTables.net plugin by including its CSS and its JS.  There are CDN versions of these listed on their site.  And that is what we’ll use.

adding dataTables CSS and JS

adding dataTables CSS and JS

Because DataTables depends on jQuery, we also added the CDN version of jQuery ABOVE the DataTables JS.

Note that because these CDN URL’s typically do not have “http:”, it may not work on local machine.  But it will work up on the server host.

Next, give our table tag an id for jQuery to reference …

<table id=”collectionTable”>

We gave it id of “collectionTable”.   Then at the bottom of our file, start a script and within jQuery’s document ready, call dataTable() on our table element …

call dataTable

call dataTable

Refresh our browser and we see that our plain HTML table has turned into this …

table enhanced with DataTables

table enhanced with DataTables

You can add styling with CSS to make it prettier.  But look at all the functionality that it gave you.  You got pagination where the user can choose from a droplist the number of items per page.  You can sort up and down each column.  And try out the search function.

This is DataTables with its default no-configuration option.  If you look in the docs, you will find that there are many more options and that it is very configurable.

No initial Sort in DataTables

You will note that the data is initially sort by the first column by default.  If you want no initial sort order.  You can do instead …

no initial sort

no initial sort

Then data will be in the order that it is in the HTML initially.  But still sortable by the user later.

Custom Sort with DataTables

DataTables enables you to provide your own sort algorithm.  We have three columns in our table.  Doing a default sort on the three column, the code will be like …

default sort

default sort

The value for sType can be “string”, numeric, date and html (which will strip HTML tags before sorting).  But we make our own called “my_custom_sort” for the first column like this …

calling my custom sort

calling my custom sort

Note that we define the asc and desc sort function into the $.fn.dataTableExt.oSort array.  Right now shown above is the normal string sort function.

But let’s say that we want the first column to be sorted such that whenever the value “sample” if found, it should be listed first in sort order.   So we alter the sort functions like the following…

custom sort function

custom sort function

Example here.  Note the if we name our custom sort to be “my_custom_sort” and the ascending and descending custom function should be name the same but with the “-asc” and “-desc” suffixed at the end.

Sort on Multiple Columns with DataTables

Suppose we want to sort first on the first column and then sort next on the second column in the event that the first column value are equal.  This is known as multi-column ordering.  We do …

multi-column sort

multi-column sort