我如何使用jQuery Tablesorter存储一种



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

以下是我的代码。

 <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>$9.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>$19.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>$15.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>$153.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>$13.19</td> 
        <td>11%</td> 
        <td>jan 18, 2007 9:12 am</td> 
    </tr> 
</tbody> 

我无法确定您是在使用Origin -tablesorter(v2.0.5)还是我的表格叉。无论哪种方式,您都需要设置sortList选项将初始排序应用于表

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

sortList使用此格式:

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

最新更新