Skip to content

Instantly share code, notes, and snippets.

@jongpak
Last active February 21, 2017 04:33
Show Gist options
  • Save jongpak/9579a4e615f561fe65704ba2f792dd76 to your computer and use it in GitHub Desktop.
Save jongpak/9579a4e615f561fe65704ba2f792dd76 to your computer and use it in GitHub Desktop.
jQuery UI - nested list exclusive drag/drop
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI - nested list exclusive drag/drop</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function() {
$('.menu').droppable({
accept: '.folder li',
drop: function(event, ui) {
$(ui.draggable).appendTo(this);
}
});
$('.folder').droppable({
greedy: true,
drop: function(event, ui) {
var srcParent = ui.draggable.closest('ul')[0];
var dstParent = $(event.target).closest('ul')[0];
if(srcParent == dstParent) {
return;
}
$(ui.draggable).appendTo(dstParent);
}
});
$('.folder li').draggable({
revert: true
});
$(".menu > li:not(:has(ul))").draggable({
revert: true,
start: function() {
console.log('d');
},
});
});
</script>
<style>
.folder.ui-droppable-hover { background: rgba(255, 255, 0, 0.5); }
.menu ul {
border: 1px solid;
margin: 5px;
}
</style>
</head>
<body>
<ul class="menu">
<li>
<ul class="folder">
<li><span style="color: blue">Folder 1-1</span></li>
<li><span style="color: blue">Folder 1-2</span></li>
<li><span style="color: blue">Folder 1-3</span></li>
<li><span style="color: blue">Folder 1-4</span></li>
<li><span style="color: blue">Folder 1-5</span></li>
</ul>
</li>
<li>
<ul class="folder">
<li><b>Folder 2-1</b></li>
<li><b>Folder 2-2</b></li>
<li><b>Folder 2-3</b></li>
<li><b>Folder 2-4</b></li>
<li><b>Folder 2-5</b></li>
</ul>
</li>
<li><span style="color: red">Item 1</span></li>
<li><span style="color: red">Item 2</span></li>
<li><span style="color: red">Item 3</span></li>
<li><span style="color: red">Item 4</span></li>
<li><span style="color: red">Item 5</span></li>
</ul>
</body>
</html>
@jongpak
Copy link
Author

jongpak commented Feb 21, 2017

jQuery의 sortable만으로는 구현하기가 힘들었다. 폴더구조의 상/하 포함관계도 있고.. 해서 draggable와 droppable을 이용하여 구현하였다.
미리보기: https://cdn.rawgit.com/jongpak/9579a4e615f561fe65704ba2f792dd76/raw/c8be25a0843a848c8ac5697294cc2090457e19e7/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment