Search code examples
javascriptjqueryjquery-uijquery-ui-draggablejquery-ui-droppable

Shopping cart - Drag and Drop bug


I have implemented jQuery's shopping card based on the example: http://jsfiddle.net/RR6z5/1/

But there is a little bug. When I drag an element from products to shopping card and then without drop, I drag from shopping card to products, the original element from Product dissapears. How to avoid this?

Thanks.

$(function () {
$("#catalog").accordion();
$("#catalog li").draggable({
    appendTo: "body",
    helper: "clone"
});
$("#cart ol").droppable({
    out: function (event, ui) {
        var self = ui;
        ui.helper.off('mouseup').on('mouseup', function () {
            $(this).remove();
            self.draggable.remove();
        });
    },
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    accept: ":not(.ui-sortable-helper)",
    drop: function (event, ui) {
        if (ui.draggable.is('.dropped')) return false;
        $(this).find(".placeholder").remove();
        $("<li></li>").text(ui.draggable.text()).appendTo(this).draggable({
            appendTo: "body",
            helper: "clone"
        }).addClass('dropped');
    }
}).sortable({
    items: "li:not(.placeholder)",
    sort: function () {
        // gets added unintentionally by droppable interacting with sortable
        // using connectWithSortable fixes this, but doesn't allow you to customize active/hoverClass options
        $(this).removeClass("ui-state-default");
    }
});


});

Solution

  • Notice that I amend something:

    if(self.draggable.parents('#cart').length){
         self.draggable.remove();
    }
    

    The query make sure that the element being dragged in #cart element, then remove it.

    If the dragged element is a original one(which must be not in #cart element but in #products element), that element won't be removed.

    $(function () {
        $("#catalog").accordion();
        $("#catalog li").draggable({
            appendTo: "body",
            helper: "clone"
        });
        $("#cart ol").droppable({
            out: function (event, ui) {
                var self = ui;
                ui.helper.off('mouseup').on('mouseup', function () {
                    $(this).remove();
                    if(self.draggable.parents('#cart').length){
                        self.draggable.remove();
                    }
                });
            },
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: ":not(.ui-sortable-helper)",
            drop: function (event, ui) {
                if (ui.draggable.is('.dropped')) return false;
                $(this).find(".placeholder").remove();
                $("<li></li>").text(ui.draggable.text()).appendTo(this).draggable({
                    appendTo: "body",
                    helper: "clone"
                }).addClass('dropped');
            }
        }).sortable({
            items: "li:not(.placeholder)",
            sort: function () {
                // gets added unintentionally by droppable interacting with sortable
                // using connectWithSortable fixes this, but doesn't allow you to customize active/hoverClass options
                $(this).removeClass("ui-state-default");
            }
        });
    
    
    });
    

    You can check it on http://jsfiddle.net/RR6z5/30/