1

I recently had help getting the correct Date format on my jQuery / tablesorter table. The client is listing companies in a table and wants one of the columns sortable by date, several of which are 'Active ' companies in the portfolio, while others have exited and those are those ones that use the date format. The problem I have, is that because these are all in the same table, and the Date column is sorting by date, the companies that are listed as 'Active' get grouped together either at the beginning or end of the list but in reverse chronologial order.

So the companies are currently sorting like 'active' > 'active' > 'active' > Jan 2006 > Mar 2010 > Feb 2012, etc.

I would like to see if there is a way to add this into the parser so that the 'Active' string is converted into the current date, so that the companies are sorted like: 'active' > 'active' > 'active' > Feb 2012 > Mar 2010 > Jan 2006.

Is this possible? Any ideas? I decided to start a new thread on this because it is an new issue from the previous, which was just to get the correct Date format working. I am posting my HTML and code below. Thanks in advance.

<table id="myTable" class="tablesorter stripeMe sample" width="100%" cellpadding="0"  cellspacing="0">
<thead>
<th width="30%" align="left">COMPANY</th><th width="35%">DESCRIPTION</th><th width="17%"  align="left">INDUSTRY</th><th width="18%" align="left">EXIT DATE</th></tr></thead>
<tbody>
<tr><td width="30%">  Cartera Commerce, Inc. </td>
<td width="35%">Provides technology-enabled marketing and loyalty solutions 
</td><td width="17%">   Financials    </td> <td width="18%">Active</td>
</tr><tr><td width="30%">   Critical Information Network, LLC </td>
<td width="35%">Operates library of industrial professional training and certification materials 
</td><td width="17%">   Education    </td> <td width="18%">Active</td>
</tr><tr><td width="30%"> Cynergydata </td>
<td width="35%">Provides merchant payment processing services and related software products 
</td><td width="17%">   Merchant Processing    </td> <td width="18%">Active</td>
</tr><tr><td width="30%">    </td>
<td width="35%">Operates post-secondary schools  
</td><td width="17%">   Education    </td> <td width="18%">Active</td>
</tr><tr><td width="30%">  CorVu Corporation</td>
<td width="35%">Develops and distributes performance management software products and related services 
</td><td width="17%">   Information Technology    </td> <td width="18%">May 2007</td>
</tr><tr><td width="30%">    Fischer Imaging Corporation </td>
 <td width="35%">Manufactures and services specialty digital imaging systems and other  medical devices
</td><td width="17%">   Healthcare    </td> <td width="18%">Sep 2005</td>
</tr><tr><td width="30%">   Global Transportation Services, Inc.  </td>
<td width="35%">Provides air and ocean transportation and logistics services
</td><td width="17%">   Transportation     </td> <td width="18%">Mar 2010</td>
</tr><tr><td width="30%">  HMP/Rita  </td>
<td width="35%">Operates as a specialty medical device company that manufactures and markets vascular and spinal access systems
</td><td width="17%">   Healthcare    </td> <td width="18%">Dec 2006</td>
</tr>

</tbody>
</table>

And the current jQuery parser:

$.tablesorter.addParser({
id: 'monthYear',
is: function(s) {
    return false;
},
format: function(s) {
    var date = s.match(/^(\w{3})[ ](\d{4})$/);
    var m = date ? date[1] + ' 1 ' || '' : '';
    var y = date && date[2] ? date[2] || '' : '';
    return new Date(m + y).getTime() || '';
},
type: 'Numeric'
});

$('#myTable').tablesorter({

headers: {
    3: {
        sorter: 'monthYear'
    }
}

});
Community
  • 1
  • 1
seanx
  • 131
  • 3
  • 11

1 Answers1

0

It's actually just a simple modification. Change the parser to this:

var today = new Date().getTime();

$.tablesorter.addParser({
    id: 'monthYear',
    is: function(s) {
        return false;
    },
    format: function(s) {
        // remove extra spacing
        s = $.trim(s.replace(/\s+/g, ' '));
        // process date
        var date = s.match(/^(\w{3})[ ](\d{4})$/),
            m = date ? date[1] + ' 1 ' || '' : '',
            y = date && date[2] ? date[2] || '' : '';
        return /active/i.test(s) ? today : new Date(m + y).getTime() || '';
    },
    type: 'Numeric'
});

The word "active" in the table cell will be case insensitive. And here is a demo of it working. Note that dates in the future, Jul 2012 in this case, will sort above "active".

Mottie
  • 84,355
  • 30
  • 126
  • 241
  • Absolutely brilliant! Thank you so much, it worked like a charm - I posted a new question because I din't want to keep bothering you necessarily, but you really came through for me - thanks again! – seanx May 13 '12 at 19:11