放入可拖动元素拖放到其他容器而不是本身?

发布于 2024-10-13 07:38:05 字数 2833 浏览 5 评论 0原文

参考了 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的产品可以拖放到其他购物车< /strong> 容器而不是容器本身,反之亦然。例如:

问题是:

当产品已放入 s*hoppingCart1* 中时,然后我可以拖动并将 shoppingCart1 中的产品拖放到其他购物车容器中。但是,当我将产品拖放到自己的产品中时,它似乎还会附加 > 产品。我的意思是,当我拖动 shoppingCart1 的产品 并放入 shoppingCart1 本身时,它也会附加到 shoppingCart1 中。

非常感谢!

我的部分代码是从 JQuery UI Droppable 重写的:购物车 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" );
                                        }
                                    });

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-10-20 07:38:05

这将确保任何列表中的内容都不会重复。
它使用 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/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文