代码之家  ›  专栏  ›  技术社区  ›  Chris

jQuery tablesorter如何查找sortList对象

  •  13
  • Chris  · 技术社区  · 14 年前

    我正在使用jQuery tablesorter插件。我想存储用户如何在页面上对表进行排序,并在下次加载页面时自动进行排序。为此,首先需要能够找到存储表排序方式的sortList对象。为了我的生命,我不知道如何得到它。文档似乎没有任何关于这方面的内容,我已经尝试了我能想到的一切。

    3 回复  |  直到 14 年前
        1
  •  28
  •   Bryan    14 年前

    您需要将表元素绑定到表排序器 sortEnd 事件。该对象的所有数据都将传递给处理程序。然后可以得到当前的排序,如下所示:

    var currentSort;
    
    $("#yourtableId").tablesorter({
        // initialization
    }).bind("sortEnd", function(sorter) {
        currentSort = sorter.target.config.sortList;
    });
    
        2
  •  1
  •   e-motiv    10 年前

    仅当您需要这样保存最后一种排序时,开销可能会少一些:

    lastSortList=$("#mytable")[0].config.sortList;
    

    当然,记住要在正确的范围内声明变量。

    (我认为提问者的问题可能是他必须通过 [0] 而不是jQuery元素。)

        3
  •  -1
  •   tijntest    10 年前

    我就是这样做到的:

    <?php
    // Set session variables
    $_SESSION["sortlistsessie"] = "[[0,0],[2,1]]";
    ?>
    
    
    <script language="javascript" type="text/javascript">
    
    //document.cookie="TestCookie3=[[0,0],[2,1]]";
    $(document).ready(function() { 
    // extend the default setting to always include the zebra widget. 
    $.tablesorter.defaults.widgets = ['zebra']; 
    // extend the default setting to always sort on the first column 
    $.tablesorter.defaults.sortList = <?php print_r($_SESSION["sortlistsessie"]
    );           ?>//      <?php $_SESSION["sortlistsessie"];?>; //<?php echo       
    $_COOKIE["TestCookie3"]; ?>; 
    // call the tablesorter plugin 
    $("#searchTable").tablesorter(); 
    }); 
    </script>