With a little side of applesauce...

Tuesday, September 14, 2010

jquery-tablesorter - not sorting by timestamp

I am finishing up my attendance application, but found that I couldn't sort my tables via a timestamp column with a 'yyyy-mm-dd hh:mm:ss.0' format. The answer? I needed to create a custom tablesorter parser:
        $.tablesorter.addParser({
id: "customDate",
is: function(s) {
return /\d{1,4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}\.\d+/.test(s);
},
format: function(s) {
s = s.replace(/\-/g," ");
s = s.replace(/:/g," ");
s = s.replace(/\./g," ");
s = s.split(" ");
return $.tablesorter.formatFloat(new Date(s[0], s[1]-1, s[2], s[3], s[4], s[5]).getTime()+parseInt(s[6]));
},
type: "numeric"
});
<cfif form.hat EQ 'eventattendance'>
$("#hor-minimalist-b").tablesorter({
sortList: [[1,1]],
headers: { 1: { sorter:'customDate' } }
});
<cfelse>
$("#hor-minimalist-b").tablesorter({
sortList: [[1,1]],
headers: { 2: { sorter:'customDate' } }
});
</cfif>



1. The addParser() definition needs to be above the tablesorter stanza.
2. I am just using Coldfusion to configure tablesorter for two different pages.

The parser was found in the links below:
http://www.weask.us/entry/date-sorting-problem-jquery-tablesorter
http://linkroller.com/fullpage/ad/13754/?http%3A//tablesorter.com/docs/example-parsers.html

No comments: