jQuery UI sortable divs across multiple containers or parents

六月ゝ 毕业季﹏ 提交于 2019-12-06 10:42:58

问题


I have a list of objects that is split into two containers that I want to make sortable via jQuery but I cannot figure out how to make one object able to be added to the other containers list and vice versa. So, I want to be able to drag an object from one container into the other and have the code treat it as if it were one list.

Here is my code below:

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery-ui/js/jquery-ui-1.8.22.custom.min.js"</script>
<link type="text/css" href="jquery-ui/css/smoothness/jquery-ui-1.8.22.custom.css" rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function(){
$("#nowplaying_list, #nowplaying_history").sortable().disableSelection();
});
</script>
</head>
<body>
<div id="left_content" class="ui-droppable">
<div id="nowplaying_container" style="display:block;">
    <div id="nowplaying_wrapper">
        <div id="nowplaying_history" class="ui-sortable">
            <div class="object_container ui-draggable" >OBJECT 1</div>
            <div class="object_container ui-draggable" >OBJECT 2</div>
            <div class="object_container ui-draggable" >OBJECT 3</div>
        </div>
        <div id="nowplaying_list" class="ui-sortable">
            <div class="object_container ui-draggable" >OBJECT 4</div>
            <div class="object_container ui-draggable" >OBJECT 5</div>
            <div class="object_container ui-draggable" >OBJECT 6</div>
        </div>
    </div>
</div>
</div>
</body>
</html>

回答1:


Take a look at the Sortable Portlets example, it appears to have the behavior you want.

The key is the connectWith option, which you could use once you assign a common class to both containers:

$(".now-playing-block").sortable({
    connectWith: '.now-playing-block'
});

(I left out the chained .disableSelection() call for clarity.)



来源:https://stackoverflow.com/questions/14574274/jquery-ui-sortable-divs-across-multiple-containers-or-parents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!