将可排序的数据删除到可排序的(嵌套的div)中

时间:2016-09-06 11:36:52

标签: jquery jquery-ui jquery-ui-draggable jquery-ui-sortable jquery-ui-droppable

我创建了一个代码,允许我将div从第1列拖动并克隆到第2列。第2列是可排序的。

这很好用,但我现在想把第1列的div拖到一个已经被放入第2列的div中。

到目前为止,这是我的代码:

HTML

<div id="column1">
    <p>Items</p>
    <div class="dragItem">Drag me ONE</div>
    <div class="dragItem">Drag me TWO</div>
    <div class="dragItem">Drag me THREE</div>
</div>
<div id="column2" class="droppable">
    <p>Drop here</p>
</div>

jQuery的:

$('.dragItem').draggable({
    helper: 'clone',
    connectToSortable: "#column2"
});

$('.dragItem').sortable({
    containment: "parent"
});

$('#column2').sortable({
    cancel: '#cont>div',
    placeholder: "highlight"
});

$('#column2').droppable({
    accept: '.dragItem',
    drop: function(event, ui) {
        var draggable = ui.draggable;
        var droppable = $(this);
        var drag = $('#column2').has(ui.draggable).length ? draggable : draggable.clone().draggable({});
        drag.appendTo(column2); 
    }
});

由于

1 个答案:

答案 0 :(得分:1)

项目被删除后 - 您需要将sortabledroppable应用于该div。

以下是一个例子:

$('.dragItem').draggable({
  helper: 'clone',
  connectToSortable: "#column2,#column2 div"
});

$('.dragItem').sortable({
  containment: "parent"
});

$('#column2').sortable({
  placeholder: "highlight"
});

$('#column2').droppable({
  accept: '.dragItem',
  drop: function(event, ui) {
    var draggable = ui.draggable;
    var droppable = $(this);
    var drag = $('#column2').has(ui.draggable).length ? draggable : draggable.clone().draggable({});
    drag.appendTo(column2); 
    drag.sortable({
      placeholder: "highlight"
    });
    drag.droppable({
      accept: ".dragItem",
      drop: function (event, ui) {
        var draggable = ui.draggable;
        var droppable = $(this);
        var drag = $('#column2').has(ui.draggable).length ? draggable : draggable.clone().draggable({});
      }
    })
    drag.css({width:'', height: ''})
  }
});
#column1, #column2 {
  width: 250px;
  margin-right: 10px;
  border: 1px solid red;
  float: left
}
.droppable div {
  border: 1px solid blue;
  margin: 5px;
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" />
<div id="column1">
  <p>Items</p>
  <div class="dragItem">Drag me ONE</div>
  <div class="dragItem">Drag me TWO</div>
  <div class="dragItem">Drag me THREE</div>
</div>
<div id="column2" class="droppable">
  <p>Drop here</p>
</div>