DataTables.net–sorting on date column

DataTables.net plug-in for jQuery is a powerful javascript library for managing HTML tables.

The following is a note on how to solve a common issue: sorting on the value of a column when the displayed value is different.  An example of this is when a date is displayed as a friendly string like Month Year:

image

The default sorting behaviour would end up with less than desirable results:

image

There are more than one way to solve this, but a simple solution is to include an additional hidden column with the date value.  Then in the column defs the column to be sorted, in this case the first column or 0 index, can reference the hidden column when sorting:

“columnDefs”: [{                       
                       “targets”: [0],
                       “dataSort”: 4 },
                           {                       
                       “targets”: [4],
                       “bVisible”: false
                       }],

The end result is correct sorting on the hidden column:

image

This entry was posted in DataTables.net, HTML, JavaScript, JQuery. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>