3

JQuery UI Droppableの例を参照した後:ショッピングカート。テスト用に書き直しましたが、問題がありました。

仮定 :

商品コンテナ:商品をショッピングカートコンテナにドラッグアンドドロップします。

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

およびショッピングカートコンテナ:

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

その他のショッピングカートコンテナ

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

そして、shoppingCart1の製品は、他のショッピングカートコンテナにドラッグアンドドロップできますが、それ自体はできません。その逆も可能です。例:

問題は:

商品がs* hoppingCart1 *にドロップされた後、shoppingCart1内の商品のショッピングカートコンテナにドラッグアンドドロップできますが、商品を独自の内部にドラッグアンドドロップすると、商品も追加されるようです。 。つまり、 shoppingCart1の商品をドラッグして、 shoppingCart1自体にドロップすると、shoppingCart1にも追加されます。

どうもありがとうございます!

コードの一部をJQueryUIから書き直しますDroppable:shopping cart exmaple![私の望みどおりに機能しません]

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

1 に答える 1

2

これにより、どのリストでも重複しないようになります。属性を使用してdata-id、各商品に商品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");
    }
});

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

于 2011-01-30T02:59:24.853 に答える