jQuery 取消拖动可排序项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5133279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:36:37  来源:igfitidea点击:

Cancel dragging of a sortable item

jqueryjquery-uijquery-ui-sortable

提问by mcmlxxxiii

An absolutely common sortable case:

一个绝对常见的可排序案例:

<script>
$(function() {
  $("#sortable").sortable();
});
</script>

<ul id="sortable">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

Problem.Need to cancel dragging of an item on some condition and there is a good advice of Andrew Whitaker, but that approach is working only for jquery-ui-draggable and fails for sortables:

问题。需要在某些条件下取消拖动项目,并且Andrew Whitaker有一个很好的建议 ,但这种方法仅适用于 jquery-ui-draggable 并且无法用于 sortables:

$("#sortable").sortable({
  start: function() {
    return false; // will still cause `this.helper is null`
  }
});

Will be greateful for suggestions.

将是伟大的建议。

采纳答案by Mottie

The sortfunction callback does the same for sort as drag for draggable (demo):

sort回调函数确实为排序为拖动(拖动同一演示):

$("#sortable").sortable({
    sort: function() {
        if ($(this).hasClass("cancel")) {
            $(this).sortable("cancel");
        }
    }
});

回答by root

Sortable has a "cancel" capability invoked using sortable('cancel').

Sortable 具有使用调用的“取消”功能sortable('cancel')

From the docs: "Cancels a change in the current sortable and reverts it to the state prior to when the current sort was started." See http://api.jqueryui.com/sortable/#method-cancel.

来自文档:“取消当前可排序的更改并将其恢复到当前排序开始之前的状态。” 请参阅http://api.jqueryui.com/sortable/#method-cancel

Example usage:

用法示例:

$("#sortable").sortable({
  stop: function(e, ui) {
    if ("I need to cancel this") {
      $(ui.sender).sortable('cancel');
    }
  }
});

回答by Ben Blank

Returning falseas fudgeysuggests works great for making things dynamically unsortable, but there's also a canceloptionin the sortable config which lets you set up static unsortables as well:

false按照fudgey 的建议返回非常适合使事物动态不可排序,但可排序配置中还有一个cancel选项可以让您设置静态不可排序:

$("#sortable").sortable({
    // this prevents all buttons, form fields, and elemens
    // with the "unsortable" class from being dragged
    cancel: ":input, button, .unsortable"
});

回答by Mj Azani

Try this example

试试这个例子

$('#list1').sortable({
    connectWith: 'ul'
});    

$('#list2').sortable({
    connectWith: 'ul',
    receive: function(ev, ui) {
       if(ui.item.hasClass("number"))
          ui.sender.sortable("cancel");
    }
});