Fixing sortForce in jQuery’s tablesorter

jQuery has a table-sorting plugin, part of their official UI project. It’s quite a nice table-sorting library, handling the common cases, with options making it configurable to suit many people’s needs.

However, I ran into a problem when using it in a project. The documentation and the functionality don’t quite line up.

It has an option, sortForce, which its documentation says you can use to “add an additional forced sort that will be appended to the dynamic selections by the user”. This is a handy concept – it lets you, say, keep records ordered by name, regardless of which other criteria the user chooses to sort by.

The problem is that it actually prepends the sort to the user’s selection, which means that the user is restricted to sorting within the forced sort. (This is also a potentially useful tool; it’s just not what the documentation indicates.)

So I wrote up a patch that fixes this, along with a few other niggling issues with sortForce. (Its interaction with the user sorting by multiple columns, and it locking the forced-sort column in one sort direction.) To preserve backwards-compatibility I added a new option, sortAppend, to provide the documented behavior.

I also submitted the patch to the maintainer, so hopefully it can get incorporated.

2008-08-27: My patch was incorporated as of version 2.0.2, so it’s all good.
2009-03-17: But bits of it weren’t applied, so it can’t be said to have been fixed. Oh well. I’ll resubmit.

Join the Conversation

4 Comments

  1. It seems like your sortappend method is not part of the tablesorter 2.0.3. If you look in the code of tablesorter “sortAppend” is only in one place where it is declared null. Therefore I can’t make it work. And the sortForce overwrites the user sorting as earlier.
    I would like to see your patch..

  2. Annoying — I didn’t notice that the patch had been mis-applied, and that the copy on my server got lost during an upgrade.

    I fixed the link in the post.

  3. #1 – Not sure how you apply .patch files in jquery..

    This isn’t another script to include?

    Secondly: You have a table with two columns, name and status.

    Al, Bob, Charlie and Zebulon all have statuses of 3, and Mark and james have statuses of 2.

    When I sort by status, will I maintain Al, Bob, Charlie and Zebulon in that order if I do a sortForce?

Leave a comment

Your email address will not be published. Required fields are marked *