Cancel dragging of a sortable item

后端 未结 4 475
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 00:16

An absolutely common sortable case:



  • Item
相关标签:
4条回答
  • 2021-02-04 01:09

    Try this example

    $('#list1').sortable({
        connectWith: 'ul'
    });    
    
    $('#list2').sortable({
        connectWith: 'ul',
        receive: function(ev, ui) {
           if(ui.item.hasClass("number"))
              ui.sender.sortable("cancel");
        }
    });  
    
    0 讨论(0)
  • 2021-02-04 01:12

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

    $("#sortable").sortable({
        sort: function() {
            if ($(this).hasClass("cancel")) {
                $(this).sortable("cancel");
            }
        }
    });
    
    0 讨论(0)
  • 2021-02-04 01:17

    Returning false as fudgey suggests works great for making things dynamically unsortable, but there's also a cancel option in the sortable config which lets you set up static unsortables as well:

    $("#sortable").sortable({
        // this prevents all buttons, form fields, and elemens
        // with the "unsortable" class from being dragged
        cancel: ":input, button, .unsortable"
    });
    
    0 讨论(0)
  • 2021-02-04 01:21

    Sortable has a "cancel" capability invoked using 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.

    Example usage:

    $("#sortable").sortable({
      stop: function(e, ui) {
        if ("I need to cancel this") {
          $(ui.sender).sortable('cancel');
        }
      }
    });
    
    0 讨论(0)
提交回复
热议问题