【发布时间】:2012-08-03 11:30:39
【问题描述】:
我想在可排序占位符中显示可排序元素位置(或索引)。它在拖动第一个元素时起作用,而不是与其他元素一起拖动(位置 0 而不是 1)。
请查看:http://jsfiddle.net/upsidown/Hw2rJ/
我做错了什么?
【问题讨论】:
标签: jquery jquery-ui jquery-ui-sortable
我想在可排序占位符中显示可排序元素位置(或索引)。它在拖动第一个元素时起作用,而不是与其他元素一起拖动(位置 0 而不是 1)。
请查看:http://jsfiddle.net/upsidown/Hw2rJ/
我做错了什么?
【问题讨论】:
标签: jquery jquery-ui jquery-ui-sortable
我一直在寻找类似挑战的解决方案,并找到了这个 http://forum.jquery.com/topic/sortable-show-placeholders-position-and-give-it-a-number 并稍加调整(和更正)就可以了
$('.elements-sortable').sortable({
placeholder: 'sortable-placeholder',
opacity: 0.6,
helper: 'clone',
sort: function(event,ui){
$(ui.placeholder).html(Number($('.elements-sortable > div:visible').index(ui.placeholder)+1));
}
});
【讨论】:
可能是因为它在您拖动的元素之前附加了新元素
让我们试试这段代码
var position;
$(".elements-sortable").sortable({
placeholder: "sortable-placeholder",
opacity: "0.6",
start: function(event, ui) {
position = position = Number($(".elements-sortable > div:not(.ui-sortable-helper)").index(ui.placeholder)+1);
$(".sortable-placeholder").html('Drop me at position ' + position);
},
change: function(event, ui) {
position = position = Number($(".elements-sortable > div:not(.ui-sortable-helper)").index(ui.placeholder)+1);
$(".sortable-placeholder").html('Drop me at position ' + position);
}
});
我已经在 jsFiddle 上测试过了
【讨论】:
在jsFiddle上试试这个
$(".connectedSortable").sortable({
connectWith: ".connectedSortable",
stop: function (e,ui ) {
$("body").append("<p> current position : " + $(ui.item).prevAll().length + "</p>");
}
});
【讨论】: