如何让jQuery Tablesorter默认排序降序?

我无法弄清楚这一点。 这个问题也在这里被问到http://www.nabble.com/TableSorter-plugin—default-column-sort-DESC-instead–How–to25180761s27240.html#a25180761没有回应。

我试过了

$.tablesorter.defaults.sortInitialOrder = 'desc'; 

并将jquery.tablesorter.js文件更改为默认为’desc’,但它不起作用。 当我单击列标题时,第一个排序仍然是升序,因此用户必须单击两次才能下降值。

如何让Tablesorter默认按降序排序?

看起来像tablesorter代码中的错误,或者我误解了sortInitialOrder参数应该做什么。 在第536行,它通过查看列已排序的次数并采用值mod 2来设置排序器顺序。它还应考虑sortInitialOrder的值。

改变第536行

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

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

并在此行之后添加(以便第一次单击其他列时为您提供默认值)

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

并从中更改第421行

 o.count = s[1]; 

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

以便在应用sortList时覆盖初始顺序。

然后,您可以使用sortInitialOrder参数对tablesorter设置列的默认第一个排序顺序。 sortList中提供的任何排序都将覆盖为整个表提供的sortInitialOrder。

请注意,这适用于Tablesorter 2.0。

尝试从tablesorter站点获得最新版本 – 这似乎在版本2.0.3和2.0.5之间修复。

  

…与最新版本的tablesorter一起使用,但没有使用我之前的版本。 希望能帮助到你!

简单地使用这个,数组中的第二项是排序顺序(0 =升序,1 =降序):

 .tablesorter({ sortList: [[0, 1]] });