我有可排序的项目列表(li)。我需要添加posability来将每个项目仅向上移动+1位置索引或向下-1位置索引。(因此每个拖动项目应该只能向上或向下移动1步)。请帮忙解决这个问题。谢谢。
答案 0 :(得分:0)
您可以在next和prev元素中添加一些类,并在停止或 beforeStop 事件中进行检查。它类似于:
$(function() {
$( "#yourlist" ).sortable({
start:function(e,ui){
$(".item2").removeClass("item2");
return true;
},
beforeStop:function(e,ui){
//checking if item2 on placeholder and mark item
},
stop:function(e,ui){
//checking, if not return false
},
sort:function(e,ui){
$(".ui-sortable-helper").next().addClass("item2");
$(".ui-sortable-helper").prev().addClass("item2");
}
});
});
答案 1 :(得分:0)
我找到了解决方案:
$("#sortable").sortable({
start: function(event, ui) {
currenPos = ui.item.index();
console.log("Old position: " + currenPos);
},
stop: function(event, ui) {
stopPos = ui.item.index();
console.log("New position: " + stopPos);
if((currenPos + 1) < stopPos || (currenPos - 1) > stopPos) {
alert("You can move only for one position up or down.");
$(this).sortable("cancel");
}
}
});
$("#sortable").disableSelection();