paul
paul

Reputation: 125

Jquery: Move a draggable element among containers

In JQuery, I am trying to drag an element from Draggable to a Droppable table with several cells, then drag the element between these cells as in this demo http://fiddle.jshell.net/c6vL61fb/4/

However, I got a problem that the element only be positioned automatically for the first time when move from draggable to droppable table, but not after that from a cell to the other cell. How can I fix the problem. Thank you very much.

HTML

<div id="products">
    <div class="ui-widget-content">
       <img id="photo1" class="product" src="http://s4.postimg.org/fh1l9dae1/high_tatras_min.jpg" width="96" height="72">            
    </div> 
</div>
<div id="cont">
    <p>Container</p>
    <table id="container" width="100%" border="1">
        <tr height="100px">
            <td id='hello1' class='hello ui-widget-content' width="50%">&nbsp;</td>
            <td id='hello2' class='hello ui-widget-content' width="50%">&nbsp;</td>
        </tr>
        <tr height="100px">
            <td id='hello3' class='hello ui-widget-content' width="50%">&nbsp;</td>
            <td id='hello4' class='hello ui-widget-content' width="50%">&nbsp;</td>
        </tr>
    </table>
</div>

Javascript

$(function () {
    $(".hello")
    .droppable({
        activeClass: "ui-state-default",
        hoverClass: "ui-state-hover",
        drop: function(event, ui) {
            var self = $(this);
            var productid = ui.draggable.attr("id");
            if (self.find("[id=" + productid + "]").length) return;                    

            $(this).append($(ui.draggable).clone());

            var cartid = self.closest('.hello').attr('id');
            $(".hello:not(#"+cartid+") [id="+productid+"]").remove();

        }
    })
    .sortable();

    $(".product").draggable({
        appendTo: 'body',
        helper: 'clone'
    });
});

Upvotes: 1

Views: 1017

Answers (1)

Mihai Matei
Mihai Matei

Reputation: 24276

You just need to remove the style when dropping the element. So the following line:

$(this).append($(ui.draggable).clone());

must be changed to

$(this).append($(ui.draggable).clone().removeAttr('style'));

Here is your updated demo

Upvotes: 2

Related Questions