【问题标题】:Jquery sortable limited movementJquery 可排序的有限移动
【发布时间】:2016-06-30 07:43:42
【问题描述】:

我有可排序的项目列表(li's)。我需要添加 posability 以仅向上移动每个项目 + 1 位置索引或向下移动 -1 位置索引。(因此每个拖动项目应该只能向上或向下移动 1 步)。请帮助解决这个问题。谢谢。

【问题讨论】:

  • 你能告诉我们你尝试过的任何代码吗?

标签: jquery jquery-ui jquery-ui-sortable


【解决方案1】:

您可以为 next 和 prev 元素添加一些类,并在 StopbeforeStop 事件中检查它。类似于:

  $(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");
        }
});
});

【讨论】:

  • 谢谢,但我找到了解决办法:
【解决方案2】:

我找到了解决方案:

$("#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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2019-10-30
    相关资源
    最近更新 更多