2010-11-10 58 views
13

我正在使用jQuery tablesorter插件。我想存储用户如何在页面上对表格进行排序,并在下次页面加载时自动进行排序。为此,我首先需要能够找到存储表的排序方式的sortList对象。对于我的生活,我无法弄清楚如何得到它。文档似乎没有任何关于此的内容,我尝试了所有我能想到的。jQuery tablesorter如何查找sortList对象

回答

28

您需要将您的表格元素绑定到tablesorter sortEnd事件。该对象的所有数据都被传递给处理程序。然后,您可以得到当前有点像这样:

var currentSort; 

$("#yourtableId").tablesorter({ 
    // initialization 
}).bind("sortEnd", function(sorter) { 
    currentSort = sorter.target.config.sortList; 
}); 
+0

这工作完美。谢谢布莱恩! – Chris 2010-11-11 14:03:29

+0

没问题。很高兴我能帮上忙。 :) – Bryan 2010-11-11 18:41:56

+0

喜欢它。喜欢它的一切。就这些 :) – HeavenCore 2013-12-03 18:07:16

1

这可能是开销少了几分保存,只有当你需要像这样的最后排序:

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

记得声明变量正确的范围。

(我觉得questioneer的问题可能是,他必须通过[0],而不是jQuery的元素得到的DOM元素。)

-1

这就是我成功地做到这一点:

<?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>