Reputation: 96484
I have two lists and the second list is sortable
and I can drag from the first list to the second list but can't figure out how to remove from the first list.
The item (li
) does get removed if I delete the helper: "clone"
but then the drag and drop action works poorly (jerks around, not smooth).
(Bonus points - also remove the left UL when empty)
Fiddle: http://jsfiddle.net/Nme9a/
<html>
<head>
<style>
#categorizer { padding: 2px; }
#list_to_process, #categories { color: blue; background-color: green; border: solid; border-width: 4px }
ul { padding: 10px; margin: 50px; float:left; list-style:none; }
li { color: yellow; padding: 25px 80px; cursor: move; }
li:nth-child(even) { background-color: #000 }
li:nth-child(odd) { background-color: #666 }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$("#categories").sortable({
revert: true
});
$("li.to_process").draggable( {
connectToSortable: "#categories",
helper: "clone",
revert: "invalid"
});
});
</script>
</head>
<body>
<div id="categorizer">
<ul id="list_to_process">
<li class="to_process" id="left1">1</li>
<li class="to_process" id="left2">2</li>
<li class="to_process" id="left3">3</li>
<li class="to_process" id="left4">4</li>
</ul>
<ul id="categories">
<li id="righta">a</li>
<li id="rightb">b</li>
<li id="rightc">c</li>
<li id="rightd">d</li>
<li id="righte">e</li>
</ul>
</div>
</body>
</html>
Upvotes: 4
Views: 974
Reputation: 1802
You can pass a callback in a "receive" option to sortable(), like so:
$(document).ready( function() {
$("#categories").sortable({
revert: true,
receive: function(evt, ui) {
ui.item.remove();
}
});
$("li.to_process").draggable( {
connectToSortable: "#categories",
revert: "invalid",
helper: "clone"
});
});
[EDIT from Michael]
Note that I also added:
$('#list_to_process li').length == 0) {
$('#list_to_process').remove();
}
after the ui.item.remove();
so that the empty UL
which still had padding and borders, i.e.
would be removed.
Upvotes: 3