10

JavaScript 应用后如何更改列表的顺序jQuery.sortable

例如:

<ul id="mylist">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

var x_sortable_list = $("mylist").sortable();

然后,假设这个工作:

x_sortable_list.changeOrder([
 {item:1, newOrder:2 },
 {item:2, newOrder:1 },
 {item:3, newOrder:3 }
]);
4

3 回答 3

16

就像其他人提到的那样,这与 sortable() 功能无关。相反,您需要检索列表中的项目,移动它们的位置,然后以新的顺序再次清除并重新插入列表项目:

// Get your list items
var items = $('#mylist').find('li');

// The new index order for each item
var order = [4, 2, 0, 1, 3];

// Map the existing items to their new positions        
var orderedItems = $.map(order, function(value) {
    return items.get(value);
});

// Clear the old list items and insert the newly ordered ones
$('#mylist').empty().html(orderedItems);
于 2010-01-14T22:01:16.030 回答
11

正如 Sparr 所说,无法sortable直接使用它们对它们进行排序,但您仍然可以使用以下方式手动移动它们:

$("#mylist li:eq(1)").insertAfter($("#mylist li:eq(2)"));
于 2010-01-14T21:30:34.827 回答
1

sortable 提供了用于重新排列项目的用户界面。要以编程方式重新排列它们,您仍然需要自己手动或使用其他插件来操作元素。

于 2010-01-14T21:21:45.287 回答