2009-10-23 34 views
16

我无法弄清楚这一点。这个问题在这里也被问到http://www.nabble.com/TableSorter-plugin---default-column-sort-DESC-instead--How--to25180761s27240.html#a25180761没有回应。如何让jQuery Tablesorter默认排序降序?

我已经试过

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

和改变jquery.tablesorter.js文件默认为“递减”,但它不工作。当我点击列标题时,第一个排序仍然是上升的,所以用户必须点击两次以降低值。

我怎样才能得到Tablesorter默认降序排序?

回答

15

看起来像一个在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; 
}); 

并从

o.count = s[1]; 

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

,使得如果施加sortlist中的初始次序被覆盖。

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

请注意,这适用于Tablesorter 2.0。

+0

是的,它的工作原理!谢谢一堆! – 2009-10-23 19:37:19

+0

非常好 - 需要在网站上的某些管理工具上执行此操作:) – 2009-11-25 12:14:47

+0

您是一名主人tvanfosson! – Aaron 2011-03-11 03:51:57

7

只需使用这一点,在阵列的第二项是排序顺序(0 =升序,1 =下降):

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

just working =) – Anubis 2012-09-24 08:11:13

21

尝试从的tablesorter站点的最新版本 - 这似乎是2.0版本之间的某处的固定.3和2.0.5。

<script type="text/javascript"> 
$(document).ready(function() 
    { 
     $("#theTable").tablesorter({ 
      sortInitialOrder: 'desc', 
      sortList: [[3,1]] // etc. 

    }); 
    } 
); 
</script> 

...与的tablesorter的最新版本的工作,但与前一个我没有。希望能帮助到你!