【发布时间】:2016-06-30 07:43:42
【问题描述】:
我有可排序的项目列表(li's)。我需要添加 posability 以仅向上移动每个项目 + 1 位置索引或向下移动 -1 位置索引。(因此每个拖动项目应该只能向上或向下移动 1 步)。请帮助解决这个问题。谢谢。
【问题讨论】:
-
你能告诉我们你尝试过的任何代码吗?
标签: jquery jquery-ui jquery-ui-sortable
我有可排序的项目列表(li's)。我需要添加 posability 以仅向上移动每个项目 + 1 位置索引或向下移动 -1 位置索引。(因此每个拖动项目应该只能向上或向下移动 1 步)。请帮助解决这个问题。谢谢。
【问题讨论】:
标签: jquery jquery-ui jquery-ui-sortable
您可以为 next 和 prev 元素添加一些类,并在 Stop 或 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");
}
});
});
【讨论】:
我找到了解决方案:
$("#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();
【讨论】: