简体   繁体   English

jQuery Sortable-将项目锁定到父级

[英]Jquery Sortable - Lock items to parent

I'm creating a list application and I've been using jQuery UI's Sortable library. 我正在创建一个列表应用程序,并且一直在使用jQuery UI的Sortable库。 I want to prevent items in a sub list from leaving that list. 我想防止子列表中的项目离开该列表。

See JS Fiddle Code Example and text below that for greater definition on my problem. 有关我的问题的更多定义,请参见JS小提琴代码示例及其下方的文本。

Here's my JS Code: 这是我的JS代码:

$(function() {
    $('.portal').sortable({
        connectWith: '.portal',
        axis: 'y',
        beforeStop: function(ev, ui) {
            if ($(ui.item).hasClass('cont') && $(ui.placeholder).parent()[0] != this) {
                $(this).sortable('cancel');
            }
        }
    });
    $('.subcont').sortable({
        connectWith: '.subcont',
        axis: 'y',
    });
    $('.cont .subcont *').sortable({
        contaiment: 'parent',
    });
});

You will see that inside of the third container, there is a table with two items inside. 您将看到在第三个容器的内部,有一个包含两个项目的表。 As their content suggests, I want to trap these two items inside of their parent container (named 'table'). 正如他们的内容所暗示的那样,我想将这两个项目捕获在其父容器(称为“表”)中。

How would I do this? 我该怎么做?

Thanks 谢谢

Fixed the issue with some fiddling on with the jQuery: 修正了一些关于jQuery的问题:

$(function() {
    $('.portal').sortable({
        connectWith: '.portal',
        axis: 'y',
        beforeStop: function(ev, ui) {
            if ($(ui.item).hasClass('cont') && $(ui.placeholder).parent()[0] != this) {
                $(this).sortable('cancel');
            }
        }
    });
    $('.cont:not(.locked) .subcont').sortable({
        connectWith: '.subcont',
        axis: 'y',
    });
    $('.cont.locked .subcont').sortable({
        connectWith: '.cont',
        axis: 'y',
    });
});

The final $.sortable() function prevents movement outside of parent. 最后的$ .sortable()函数可防止移出父级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM