Tablesorter is a brilliant jQuery plugin for allowing HTML table to be sorted within the browser via javascript. The biggest issue I have is the lack of documentation of the data types to force columns to be sorted correctly. So here they are.
- text
- digit
- integer
- currency
- floating
- ipAddress
- url
- isoDate
- percent
- usLongDate
- shortDate
- time
I credit Terminally Incoherent for posting these. I am adding this to my blog so I can find it easier and possibly others can as well.
I am also adding some code for working with currency columns by way of a custom parser.
You will need to include the following for jQuery.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
(this will change with time and versions)
As well as have the tablesorter library available. You can down load this from the tablesorter website above and below and host it locally on your site. Update the code below to reflect where you put it.
<script type="text/javascript" src="{/path/to/}jquery.tablesorter.js"></script>
After
$(document).ready(function(){
add the following.
$.tablesorter.addParser({
id: 'currency-column',
is: function(s) {
return false;
},
format: function(s) {
s = s.replace(/$/g,"");
s = s.replace(/\(/g,"-");
s = s.replace(/\)/g,"");
s = s.replace(/,/g,"");
return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9.-]/g),""));
},
type: 'numeric'
});
Then use it like this.
$("table").tablesorter({
headers:
{
0 : { sorter: 'currency-column' }
}
});
The columns are counted 0,1,2,… etc so the “0″ above is the first column. You would use the number of the column you want sorted this way.
For more info about tablesorter go here.