jQuery tablesorter如何查找sortList对象

时间:2021-12-17 03:36:04

I am using the jQuery tablesorter plugin. I am wanting to store how a user has sorted the table on the page and automatically sort that way the next time the page loads. To do this, I first need to be able to find the sortList object that stores how the table is sorted. For the life of me I cannot figure out how to get it. The documentation doesn't seem to have anything on this and I've tried everything I can think of.

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

3 个解决方案

#1


29  

You need to bind your table element to the tablesorter sortEnd event. All the data for that object is passed in to the handler. You can then get the current sort like so:

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

var currentSort;

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

#2


1  

It might be a bit less overhead to save the last sort only when you need it like this:

只有当你需要它时,保存最后一个排序可能会少一些开销:

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

Remember to declare the variable in the right scope of course.

记得在适当的范围内声明变量。

(I think the questioneer's problem probably was that he had to get the DOM element via [0] and not the jQuery element.)

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

#3


-1  

this is how I managed to do it:

这就是我设法做到的方式:

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

#1


29  

You need to bind your table element to the tablesorter sortEnd event. All the data for that object is passed in to the handler. You can then get the current sort like so:

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

var currentSort;

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

#2


1  

It might be a bit less overhead to save the last sort only when you need it like this:

只有当你需要它时,保存最后一个排序可能会少一些开销:

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

Remember to declare the variable in the right scope of course.

记得在适当的范围内声明变量。

(I think the questioneer's problem probably was that he had to get the DOM element via [0] and not the jQuery element.)

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

#3


-1  

this is how I managed to do it:

这就是我设法做到的方式:

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