7

I use jQuery ui draggable but the option appendTo doesn't work. However other option like containment or grid work properly. Here is the code:

HTML

<div id="demo">
</div>
<div id="sidebar">
    <div class="item">
    </div>
    <div class="item">
    </div>
    <div class="item">
    </div>
    <div class="item">
    </div> 
</div>

script

jQuery(".item").draggable({ appendTo: "#demo", grid: [ 10, 10 ], containment: "#demo" });

CSS

#demo {
    width: 500px;
    height: 500px;
    border: 1px solid black;
    float: left;
}
#draggable {
    background: red;
    width: 50px;
    height: 50px;
}
#sidebar {
    float: left;
    width: 300px;
}
.item {
    cursor: pointer;
    background: black;
    width: 100px;
    height: 100px;
    margin: 10px;
}

Here is a live demo: http://jsfiddle.net/fzjev/

MatthewK
  • 466
  • 4
  • 9
  • 19

3 Answers3

10

here's an open bug on the issue - Draggable: appendTo option doesn't work together with helper: 'original'.

The suggested workaround is to use helper: 'clone' and hide/show the original on drag start/stop.

e.g.

$('.draggyThing').draggable({
        appendTo: '.dropArea',
        helper: 'clone',
        start: function (event, ui) {
            $(this).hide();
        },
        stop: function (event, ui) {
            $(this).show();
        }
    });

You'll probably then want to manually append your draggables on drop of a droppable element.

 $('.dropArea').droppable({
        accept: '.draggyThing',
        drop: function (event, ui) {
            $('.dropArea').append(ui.draggable);
        }
    });
RYFN
  • 2,939
  • 1
  • 29
  • 40
  • This actually seems like more of an answer to the OP's question and a solution to having the same behaviour as what would be expected otherwise. – Arjun Mehta Oct 07 '13 at 21:09
  • 1
    Jquery Sortable works the same way, but sortable is initiated on the container element of the sortable items, so you would want to use `ui.item.hide()/show()` instead of `$(this)` in that case – Kristian Sandström Feb 04 '15 at 09:04
4

It looks like for the appendTo option to be recognized the item being dragged has to be outside of the body.

From jQuery UI 1.8.18 (around line 275)

if(!helper.parents('body').length)
  helper.appendTo((o.appendTo == 'parent' ? this.element[0].parentNode : o.appendTo));

In your example the if statement evaluates to false, so the appendTo logic is not fired.

Ian Robinson
  • 16,892
  • 8
  • 47
  • 61
  • 1
    http://jsfiddle.net/fzjev/ Drag an item, you will see that it is draggable inside the #demo but the
    doesn't append inside the #demo.
    – MatthewK Mar 10 '12 at 16:40
  • Eclipsed my previous answer (which was really more of a question) and updated with something more answer-like. I'm honestly not sure why it's set up as it is, but at least we know what's going on now. – Ian Robinson Mar 10 '12 at 18:42
0

In my case, work fine these example:

$("[drag='true']").draggable({
    drag: handleDragDrag,
    start: handleDragStart,
    stop: handleDragStop,
    helper: 'clone',
    appendTo: 'body'
})
function handleDragStart(ev, ui) {
    $(this).show();
}
function handleDragDrag(ev, ui) {
}
function handleDragStop(ev, ui) {
    $(this).show();
}
lmcDevloper
  • 332
  • 2
  • 8