qinHaiXiang
qinHaiXiang

Reputation: 6419

Dropped in draggable elements drag and drop to other containers but not itself?

After referenced the JQuery UI Droppable example : Shopping cart. I rewrite it for a test.But got some problem.

Suppose that :

product container: drag and drop the product into shopping cart container.

<div class="products">
  <ul>
    <li> product 1 </li>
    ...
  </ul>
</div>

and shopping cart container:

<div class="shoppingCart1">
... dropped products here
</div>

and others shopping cart container:

<div class="shoppingCar2">
... dropped products here
</div>

And I want the shoppingCart1's products can drag and drop to other shopping cart container but not itself,and vice versa.E.g:

the problem is:

When the products had been dropped in s*hoppingCart1*, and then I can drag and drop the products in shoppingCart1 to other shopping cart container.But it seems that when I drag and drop the products inside of its own, it also append the products. I mean when I drag the shoppingCart1's product and drop in shoppingCart1 itself,it appended to shoppingCart1,too.

Thank you very much!

part of my code re-write from JQuery UI Droppable :shopping cart exmaple![ doesn't work as my want ]

    this.igtoMDMovieList = $('<div />',{'class':'igtoMDMovieList'})
                            .appendTo(this.igtoMDMovieListWrapper);


        this.igtoMDMovieListOL = $('<ol />',{'class':'igtoMDMovieListOL'})
                                    .html('<li class="placeholder">Add your items here</li>')
                                    .appendTo(this.igtoMDMovieList)
                                    .droppable({
                                        accept:'li', 
                                        drop: function( event, ui ) {

                                            $( "<li></li>" )
                                                .text( ui.draggable.text() )
                                                .appendTo(obj.igtoMDMovieListOL)//$(this).children()
                                                .draggable({
                                                    appendTo: "#desktopFrame",
                                                    helper: "clone"
                                                })

                                        }
                                    })
                                    .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" );
                                        }
                                    });

Upvotes: 3

Views: 4784

Answers (1)

PetersenDidIt
PetersenDidIt

Reputation: 25620

This will make sure things don't get duplicated in any list. It uses the data-id attribute to give each product a product id.

$("#products li").draggable({
    appendTo: "body",
    helper: "clone"
});
$(".shoppingCart ol").droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    drop: function(event, ui) {
        var self = $(this);
        self.find(".placeholder").remove();
        var productid = ui.draggable.attr("data-id");
        if (self.find("[data-id=" + productid + "]").length) return;
        $("<li></li>", {
            "text": ui.draggable.text(),
            "data-id": productid
        }).appendTo(this);
        // To remove item from other shopping chart do this
        var cartid = self.closest('.shoppingCart').attr('id');
        $(".shoppingCart:not(#"+cartid+") [data-id="+productid+"]").remove();
    }
}).sortable({
    items: "li:not(.placeholder)",
    sort: function() {
        $(this).removeClass("ui-state-default");
    }
});

Example: http://jsfiddle.net/petersendidit/S4QgX/

Upvotes: 2

Related Questions