开发者

make jQuery table sorter sort in ascending order everytime a new column is clicked

开发者 https://www.devze.com 2023-04-12 11:15 出处:网络
I would like to ask if there\'s a way to make table sorter sort in ascending order开发者_Python百科 everytime a new column is clicked?

I would like to ask if there's a way to make table sorter sort in ascending order开发者_Python百科 everytime a new column is clicked?

thanks!


If you want the columns to only sort by their initial sort direction, find and comment out this line in jquery.tablesorter.js:

this.order = this.count++ % 2;

This is the line which updates the column to sort in the opposite direction the next time the header is clicked. Commenting this out means that the sort order will always be the same. But it also means that you won't be able to sort in the opposite direction.

I had a situation (which may be the same as you described) where I wanted each column to sort in the initial sort direction the first time I clicked, but still wanted to be able to sort the other direction when I clicked again. eg. Clicking Column A would sort ASC, then clicking again would sort DESC then again ASC. Clicking B would sort ASC, then clicking A again would sort ASC. With the original tablesorter code, that last sort would be DESC, since the Column A sort of the previous click was ASC.

Here is how I solved that. It may be kludgy and clumsy, but it works for me.

In the buildHeaders function, find this line:

this.order = formatSortingOrder( checkHeaderOrder(table, index) );

and add this after it:

this.initialOrder = formatSortingOrder( checkHeaderOrder(table, index) );

Replace the line I mentioned earlier:

this.order = this.count++ % 2;

With this:

if($lastColumn == i){
    this.order = ++this.count % 2;
}else{
    this.order = this.initialOrder;
}
$lastColumn = i;

Now, say Column A's sortInitialOrder was ASC and B was DESC. The following would happen:

  1. Click A: Sorts A ASC
  2. Click B: Sorts B DESC
  3. Click B: Sorts B ASC
  4. Click A: Sorts A ASC


Here's a fork of tablesorter where you can pass a lockedOrder parameter: http://mottie.github.io/tablesorter/docs/example-options-headers-locked.html

0

精彩评论

暂无评论...
验证码 换一张
取 消