Using jQuery tablesorter to sort mm / yy dates

I am using the jquery tablesorter plugin to sort a table. On my columns in my table, the date is displayed in mm / yy format.

<tr> <td class="col-name">...</td> ... <td rel="2000" class="col-dob">10/00</td> ... </tr> <tr> <td class="col-name">...</td> ... <td rel="1986" class="col-dob">11/86</td> ... </tr> 

Note:

  • Each cell has a unique class.
  • Date is displayed in mm / yy format.
  • Date cell also gets year.

My jQuery code is as follows:

 // add parser through the tablesorter addParser method $.tablesorter.addParser({ // set a unique id id: 'user-birthdate', is: function(s) { // return false so this parser is not auto detected return false; }, format: function(s) { // format your data for normalization var dateSplit = s.split('/'); if(2 !== dateSplit.length) return 0; return new Date(dateSplit[1], dateSplit[0], 1); }, // set type, either numeric or text type: 'numeric' }); myClass.init = function() { $('.module .user table').tablesorter({ sortList: [[0,0]], widgets: ['zebra'], headers: { 5: { sorter:'user-birthdate' } } }); } myClass.init(); 

My problem is that tableSorter interprets 00 as the year 1900 instead of 2000, and therefore the sorted data is incorrect.

Any clue how can I solve this? I am using jQuery 1.2.6 and the latest version of tablesorter.

+6
jquery tablesorter
source share
2 answers

The tablesorter documentation is often quite useless, I found. He seems to be talking a lot, but lacking in detail.

In this case, it does not tell you the function signature for the parser. Fortunately, you can read the uninstalled code to find it.

Here we find that the metadata parser does this:

 format: function(s,table,cell) { 

This means that you can customize your format:

 format: function(s, table, cell) { // format your data for normalization var dateSplit = s.split('/'); var year = $(cell).attr('rel'); if(2 !== dateSplit.length) return 0; return new Date(year, dateSplit[0], 1); }, 

Or at least it looks like this. I have not really tested this. But it should be at least very close.

+9
source share

I think you will find that your problem is the Date constructor and the 2-digit year string that you pass without a value: new Date(dateSplit[1], dateSplit[0], 1);

I don't think you can (easily) access s-based rel in the parser. Does s contain all the contents of the cell? Can you do something in the data in the cell, for example: <span style="display : none">CC</span>MM/YY , cross out the tags and then combine CC with YY in your analysis?

0
source share

All Articles