问题
I have 2 connected sortable lists. One is inside an accordion. When I try to drag items from the sortable in the accordion, the helper disappears as soon as I get outside of the accordion. I can drop to one of the other connected sortables and the item will display, but it just doesn't display while I'm dragging. The accordion also scrolls down if I drag and item down.
I can drag & drop items from either of the other list wherever I need and it works fine. How can I make items not disappear while dragging them from inside an accordion to the outside of it?
I've already tried the containment option but that seems to have no effect.
Here's code to expose the problem that I've taken from these examples: http://jqueryui.com/demos/sortable/#connect-lists
I want to be able to drag items from the accordion into the sortable list. I can actually drop them into the list, but they disappear while I'm dragging them outside of the accordion.
<html>
<head>
<title>Accordion Sortable Failure Test</title>
<link type="text/css" href="css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet"/>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript">
$(function(){
$(".sortable").sortable({connectWith: ".sortable"});
$("#accordion").accordion({ header: "h3" });
});
</script>
</head>
<body>
<h2>Sortable</h2>
<ul class="sortable">
<li>Row 1</li>
<li>Row 2</li>
</ul>
<h2>Accordion</h2>
<div id="accordion">
<div>
<h3><a href="#">First</a></h3>
<ul class="sortable">
<li>Lorem</li>
<li>ipsum</li>
<li>dolor</li>
</ul>
</div>
<div>
<h3><a href="#">Second</a></h3>
<div>Phasellus mattis tincidunt nibh.</div>
</div>
<div>
<h3><a href="#">Third</a></h3>
<div>Nam dui erat, auctor a, dignissim quis.</div>
</div>
</div>
</body>
</html>
回答1:
In your sortable call you want to use the following options:
helper: "clone",
appendTo: "body", // or whatever element you want the helper clone to be attached to
This does two things. First it makes a copy of the element being dragged (helper option), second it attaches that helper to the specified element (appendTo option).
Similar question here: jQuery-Ui: Cannot drag object outside of an accordion
回答2:
I'm a little late, I know, but I had a similar problem with two sortable accordions where you can drag&drop items between the two accordions.
So, for future reference:
I also couldn't get the accordion-items to "go out" of the source-accordion, and google led me to this question.
I solved my problem by adding axis: undefined on the sortable() function:
$("#accordion1")
.accordion({
collapsible: true,
header: "> div > h3",
dropOnEmpty: true,
autoHeight: false,
active: false
})
.sortable({
axis: "y",
handle: "h3",
stop: function() {
stop1 = true;
},
connectWith: '.connectedSortable',
helper: 'clone',
axis: undefined
});
Now the accordion-items can be dragged all over the place.
回答3:
paste a sample of your code so we know what you have left out... it seems that while you are on mouse down... then your accordion fires an event... i could be wrong but sounds like it... so try and have a look at propagation and stopPropagation on jquery.
来源:https://stackoverflow.com/questions/2040359/jquery-sortable-cannot-be-dragged-outside-of-accordion