How to get jQuery Tablesorter to sort descending by default?

前端 未结 3 529
半阙折子戏
半阙折子戏 2021-02-07 09:43

I can\'t figure this out. This question was also asked here http://www.nabble.com/TableSorter-plugin---default-column-sort-DESC-instead--How--to25180761s27240.html#a25180761 wi

3条回答
  •  后悔当初
    2021-02-07 10:12

    Looks like a bug in the tablesorter code, or I'm misunderstanding what the sortInitialOrder parameter is supposed to do. At line 536 it sets the sorter order by looking at the number of times the column has been sorted and taking the value mod 2. It should also take into account the value of sortInitialOrder.

    Change line 536 from

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

    to

    this.order = this.count++ == 0 ? this.order : (1 - this.order);
    

    And add after this line (so that the first click on a different column gives you the default)

    $headers.not($cell).each( function() {
        this.count = 0;
    });
    

    and change line 421 from

    o.count = s[1];
    

    to

    o.order = o.count = s[1];
    

    so that the initial order is overridden if a sortList is applied.

    Then you can use the sortInitialOrder parameter to tablesorter to set up a default first sort order for the column. Any ordering provided in the sortList will override the sortInitialOrder provided for the entire table.

    Note that this applies to Tablesorter 2.0.

提交回复
热议问题