tablesorter icon indicating copy to clipboard operation
tablesorter copied to clipboard

How to always sord DESC on first click

Open ghost opened this issue 8 years ago • 6 comments

Hello, the plugin is awesome :+1: but I need a little tune. Please help :smile: Now, if you click the first time on TH - sorting is ASC, the second click - it's DESC. I need always first click to be a DESC on every new TH. And if I click again on the same TH - than do ASC. How can I do that?

ghost avatar Aug 20 '16 21:08 ghost

There is a sortInitialOrder option that isn't listed in the documentation. Set it to "desc" or 1.

$(function(){
  $('.tablesorter').tablesorter({
    sortInitialOrder: 'desc'
  });
});

See this blog post for more undocumented options.

Mottie avatar Aug 21 '16 15:08 Mottie

@Mottie Cool, thanks, that works! Can you help me with second part of the question? I would like to have this behavior: always sort desc on first click of another column. If you click second time same column, then do wise-versa, e.g. acs

ghost avatar Aug 25 '16 14:08 ghost

There isn't an easy method in place with this version of tablesorter. I do have a fork of tablesorter with a sortRestart option that will do exactly what you are needing - demo.

Mottie avatar Aug 25 '16 16:08 Mottie

@Mottie wow, that's awesome! Thanks!

upd: mate, if I could rise ur karma, I would do it :heart: thanks a lot!

ghost avatar Aug 26 '16 08:08 ghost

@Mottie If you haven't already you should open a PR for sortRestart — exactly what I'm looking for too.

cshold avatar Aug 31 '16 12:08 cshold

Actually, now that I think about it, you could just bind to the "sortEnd" event (demo)

$('table')
  .on('sortEnd', function() {
    var c = this.config,
      th = c.headerList,
      initOrder = /^(a|0)/.test(c.sortInitialOrder) ? 0 : 1;
    if (c.sortRestart) {
      $.each(th, function(indx) {
        // don't restart the sort on already sorted columns
        if (!/headerSort/.test(this.className)) {
          this.count = initOrder;
        }
      });
    }
  })
  .tablesorter({
    sortInitialOrder: 'desc', // or 1
    sortRestart: true
  });

Mottie avatar Aug 31 '16 16:08 Mottie