我如何使用 jquery tablesorter 存储排序?

how can i store a sort using jquery tablesorter?

我正在使用 jquery table分拣机。我可以对所有列进行排序,但我想在下次访问该页面时存储一个排序类型。我怎样才能做到这一点?因为我还想在加载时按升序对 table 进行排序,让我们假设是第一次或者没有进行排序。

以下是我的代码。

 <script>
 $(document).ready(function() { 
// call the tablesorter plugin 
$("table").tablesorter({ 
    // change the multi sort key from the default shift to alt button 
    sortMultiSortKey: 'altKey' 
    }); 
}); 

</script>

HTML如下

 <table cellspacing="1" class="tablesorter">             
<thead>> 
    <tr> 
        <th>first name</th> 
        <th>last name</th> 
        <th>age</th> 
        <th>total</th> 
        <th>discount</th> 
        <th>date</th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
        <td>peter</td> 
        <td>parker</td> 
        <td>28</td> 
        <td>.99</td> 
        <td>20%</td> 
        <td>jul 6, 2006 8:14 am</td> 
    </tr> 
    <tr> 
        <td>john</td> 
        <td>hood</td> 
        <td>33</td> 
        <td>.99</td> 
        <td>25%</td> 
        <td>dec 10, 2002 5:14 am</td> 
    </tr> 
    <tr> 
        <td>clark</td> 
        <td>kent</td> 
        <td>18</td> 
        <td>.89</td> 
        <td>44%</td> 
        <td>jan 12, 2003 11:14 am</td> 
    </tr> 
    <tr> 
        <td>bruce</td> 
        <td>almighty</td> 
        <td>45</td> 
        <td>3.19</td> 
        <td>44%</td> 
        <td>jan 18, 2001 9:12 am</td> 
    </tr> 
    <tr> 
        <td>bruce</td> 
        <td>evans</td> 
        <td>22</td> 
        <td>.19</td> 
        <td>11%</td> 
        <td>jan 18, 2007 9:12 am</td> 
    </tr> 
</tbody> 

我无法判断您使用的是原始 tablesorter (v2.0.5) 还是我的 tablesorter 分支。无论哪种方式,您都需要设置 sortList option 将初始排序应用于 table

$(function(){
  $("table").tablesorter({
    sortList : [[1,0], [2,1]] // initial sort columns (2nd and 3rd)
  });
});

sortList使用这种格式:

[[columnIndex, sortDirection], ... ]
  • columnIndex 是从零开始的列索引。
  • sortDirection 设置为 0(零)应用升序排序,1(一)应用降序排序。
  • 将多列排序添加为数组中的数组。因此,上面的示例将对第二列应用升序排序,然后对第三列应用降序排序。