我在我的一个项目中使用了类似的功能,并且我已经准备好一段代码来恢复排序。该函数有 2 个参数:
- 列表容器
- 保存逗号分隔 ID 的 cookie。
我用它来重新排序基于 ul 的列表,但在你的情况下也应该可以正常工作......
// Function that restores the list order from a cookie
function restoreOrder( _list, _cookie ) {
var list = $( '#' + _list );
if( list == null ) return;
// fetch the cookie value (saved order)
var cookie = $.cookie( _cookie );
if( !cookie ) return;
// make array from saved order
var IDs = cookie.split( "," );
// fetch current order
var items = list.sortable( "toArray" );
// make array from current order
var rebuild = new Array();
for( var v = 0, len = items.length; v < len; v++ )
rebuild[items[v]] = items[v];
for( var i = 0, n = IDs.length; i < n; i++ ) {
// item id from saved order
var itemID = IDs[i];
if( itemID in rebuild ) {
// select item id from current order
var item = rebuild[itemID];
// select the item according to current order
var child = $( '#' + _list ).children( '#' + item );
// select the item according to the saved order
var savedOrd = $( '#' + _list ).children( '#' + itemID );
// remove all the items
child.remove();
// add the items in turn according to saved order
// we need to filter here since the "ui-sortable"
// class is applied to all ul elements and we
// only want the very first! You can modify this
// to support multiple lists - not tested!
$( '#' + _list ).filter( ':first' ).append( savedOrd );
} // if
} // for
} // restoreOrder
我忘记了我是从哪里得到它的(谷歌搜索时出现的一些论坛或博客)......但应归功于原作者。我通过接受这些参数对原始例程进行了一些修改,以使其更易于重用。
干杯,微观世界^地球