Thoughts on life, liberty, and information technology

TableKit and default sort ascending

A recent post on the dexagogo newsgroup asked this question regarding TableKit, the JavaScript table enhancement:

… Is it possible, when you first click on a column to sort, to sort that column in ASC order instead of DESC like it now does?

Currently, when you click on a sortable column, the default sort order is descending (z-a). I would instead like to have it sort by ascending on first click (a-z).

Lucky for the guy who posted that message, I just recently modified a local copy of TableKit to do this. Around line 330, change this line:

order = order ? order : (cell.hasClassName(op.descendingClass) ? 1 : -1);

to this:

order = order ? order : (cell.hasClassName(op.descendingClass) ? 1 :
(cell.hasClassName(op.ascendingClassName) ? -1 : (cell.hasClassName('initialSortAsc') ? 1 : -1))
);

Then, in your HTML markup, add the class initialSortAsc to the column header, as in this:


...

Essentially the change does this:

  • If the cell has the descending class name, then sort ascending.
  • Otherwise, if it has the ascending class name, then sort descending.
  • Otherwise, if it has the class name initialSortAsc, then sort ascending.
  • Otherwise, sort descending.

In other words — by applying the class ‘initialSortAsc’ to your header row, it will make the default sort for that column ascending.

Leave a comment