[英]jQuery Sortable List Not Reordering on create
我正在尝试根据列表中的元素顺序在创建可排序的jQuery时重新排序列表中的元素。
var CACHES = ["WayBack Machine", "Google Cache", "Coral CDN"];
var HTML = ["Google Cache", "WayBack Machine", "Coral CDN"];
$("#sortable").sortable({
stop: function(event, ui) {
console.log("New position: " + ui.item.index());
},
change: function(event, ui) {
// When the item postion is changed while dragging a item, I want it's position
console.log("New position: " + ui.item.index());
},
create: function(event, ui) {
console.log("Sortable Created");
var ul = $('#sortable');
var li = ul.children('li').get();
li.sort(function(a,b) {
$(a).attr('id')
var value = CACHES.indexOf($(a).attr('id')) < CACHES.indexOf($(b).attr('id')) ? 1 : -1
console.log(value)
return value
});
ul.append(li);
}
});
var HTML是它们在页面HTML中的顺序。 var CACHES是我要放入的顺序。它似乎确实对元素进行了重新排序,但不正确。
http://www.w3schools.com/jsref/jsref_sort.asp
使用-代替<和三元
var value = CACHES.indexOf($(a).attr('id')) - CACHES.indexOf($(b).attr('id'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.