How do I force jQuery tablesorter to always sort a column in one direction?
I have a sorted table of results for a sporting event. When I click on a title, I want to sort from best to worst, always. I don't want it to change when I press again. In some cases, the "best" is the shortest amount of time, and in other cases, the "best" is the largest amount (ie, greatest weight, greatest distance). So far, I have had no luck.
a source to share
Never used a tablesorter before, but without looking down it seems like it does what you want it to do it out of the box. Here are a few (untested) hacks I came up with:
One way (without modifying the plugin source) is to select this example and add a click event handler to the header cells to trigger sorting using a specific order. You may need to remove the handler added by the tablesorter plugin, or return false from your handler to avoid sorting twice.
Alternatively, if you're not afraid to change the plugin source, it looks like line 536 is what you want:
// get current column sort order
this.order = this.count++ % 2;
Replace this.count++ % 2
with 0
for ascending, 1
for descending. You can also make it more robust by adding "force ordering" as an option (see lines 94-114), perhaps even turning it into an array (for example sortList
) to specify which columns should be force-ordered.
Good luck!
a source to share