简体   繁体   English

可排序列表中的 JQuery 可放置 div

[英]JQuery droppable div inside a sortable list

I'm trying to create a simple sortable and draggable list composed of :我正在尝试创建一个简单的可排序和可拖动的列表,其中包括:

  • a list of draggable item (linked to the sortable below)可拖动项目列表(链接到下面的排序)
  • a sortable list of item一个可排序的项目列表
    • Some of those item will have a droppable div inside of them其中一些项目内部会有一个可放置的 div

I managed to do this so far : JSFiddle到目前为止,我设法做到了这一点: JSFiddle

HTML Code : HTML代码:

<h3>DRAGGABLE</h3>
<ul id="draggables">
  <li class="draggable">Item 1</li>
  <li class="draggable">Item 2</li>
  <li class="draggable">Item 3</li>
  <li class="draggable">Item 4</li>
  <li class="draggable">Item 5</li>
</ul>

<h3>SORTABLE</h3>
<ul id="itemsContainer">
  <li class="item">Item 1</li>
  <li class="item">
    <p>Item with drop zone</p>
    <div class="droppable">DROP HERE</div>
  </li>
  <li class="item">Item 3</li>
  <li class="item">Item 4</li>
  <li class="item">Item 5</li>
</ul>

The JS Code is here : JS代码在这里:

$(".draggable").draggable({
  connectToSortable: '#itemsContainer',
  helper: 'clone',
  revert: 'invalid'
});

$("#itemsContainer").sortable({
  revert: true
});

$(".droppable").droppable({
  drop: function(event, ui) {
    $(this).html("Dropped!");
    $(this).css("background-color", "red");
    $(ui.helper[0]).css("background-color", "green");
  }
});

The problem is, when an item is dropped inside the droppable div, it detects correctly that a drop is happening, but the item is still added to the sortable list.问题是,当一个项目被放置在可放置的 div 中时,它会正确检测到正在发生放置,但该项目仍被添加到可排序列表中。 I thought the "greedy" option would prevent this from happening.我认为“贪婪”选项可以防止这种情况发生。 Any ideas ?有任何想法吗 ?

EDIT : If I apply a styling to the ui.helper[0] object (aka the cloned draggable object), it stays on until the item is actually sorted in the sortable list.编辑:如果我对 ui.helper[0] 对象(又名克隆的可拖动对象)应用样式,它会一直保持,直到项目在可排序列表中实际排序。

Might be a bit of overkill, but this is what I could come up with.可能有点矫枉过正,但这是我能想到的。

When we stop dragging a draggable item, we need to see if the item is over a droppable area, if so append it to said area.当我们stop拖动可拖动项目时,我们需要查看该项目是否在可放置区域上,如果是,则将其附加到该区域。

Edit: Now we can drop a sortable item from the same list into the drop zone by connecting the sortable list to the drop zone and accepting the sortable items.编辑:现在我们可以通过将可排序列表连接到拖放区并接受可排序项目,将同一列表中的可排序项目拖放到拖放区中。

jsFiddle js小提琴

 var over = false; // is the item over a droppable area? var el_over = null; // if over a drop area, what drop area? var de = null; // the item to detach $(".draggable-item").draggable({ connectToSortable: ".list-2", stop: function(event, ui) { if (over) { de = $(this).detach(); el_over.append(de); } over = false; el_over = null; } }); $(".list-2").sortable({ connectWith: '.drop-zone, .list-2', cursor: "move", stop: function(event, ui) { if (over) { de = ui.item.detach(); el_over.append(de); } over = false; el_over = null; } }); $(".drop-zone").droppable({ accept: ".draggable-item, .sortable-item", over: function(event, ui) { //console.log("over"); over = true; el_over = $(this); }, out: function(event, ui) { //console.log("out"); over = false; el_over = null; } });
 ul { padding: 10px; list-style-type: none; width: 200px; } li { text-align: center; padding: 5px 10px; margin: 5px; } .draggable-item { background: #9bcdf0; } .sortable-item { background: #6c2020; } .drop-zone { min-height: 30px; background: #fff; padding: 1px 0; } .drop-zone .draggable-item { width: auto !important; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <ul class="list-1"> <li class="draggable-item">draggable 1</li> <li class="draggable-item">draggable 2</li> <li class="draggable-item">draggable 3</li> <li class="draggable-item">draggable 4</li> <li class="draggable-item">draggable 5</li> </ul> <ul class="list-2"> <li class="sortable-item">sortable 1</li> <li class="sortable-item">sortable 2</li> <li class="sortable-item">sortable 3 <div class="drop-zone"></div> </li> <li class="sortable-item">sortable 4</li> <li class="sortable-item">sortable 5</li> </ul>

Updated answer: Turns out we don't need all that overkill XD.更新的答案:事实证明我们不需要所有那些矫枉过正的 XD。 If we just initialize the .drop-zone as a sortable and connect it to itself and list-2 we get the same outcome.如果我们只是将.drop-zone初始化为可排序的并将其连接到它自己和list-2我们会得到相同的结果。

Here is the updated fiddle .这是更新的小提琴

$(".draggable-item").draggable({
    connectToSortable: ".list-2",
});

$(".list-2").sortable({
    connectWith: '.drop-zone, .list-2',
    cursor: "move",
});

$(".drop-zone").droppable({
    accept: ".draggable-item, .sortable-item",
});

$(".drop-zone").sortable({
    connectWith: '.drop-zone, .list-2',
  items: '.draggable-item, .sortable-item',
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM