NestedSortable:当使用connectWith()时,isAllowed()不起作用

时间:2013-10-20 02:21:17

标签: nested-sortable

我正在使用带有isAllowed condiction的nestedSortable:

    $('ol.nestedSortable').nestedSortable({
        forcePlaceholderSize: true,
        handle: 'div',
        helper: 'clone',
        connectWith: 'ol.item-tree', //!
        items: 'li',
        opacity: .6,
        placeholder: 'placeholder',
        revert: 250,
        tabSize: 25,
        tolerance: 'pointer',
        toleranceElement: '> div',
        maxLevels: 0,

        isTree: true,
        expandOnHover: 700,
        startCollapsed: true,

        isAllowed: function (item, parent) {
            if (!parent.attr('accepttypes')) {
                return true;
            }

            if (parent.attr('accepttypes') == "none") {
                return false;
            }

            if (parent.attr('accepttypes').indexOf(item.attr('type')) >= 0) {
                return true;
            }

            return false;
        },
        ....

但isAllowed仅适用于单个树。当被拖入另一个树(由connectWith定义时,即使条件不满足,也可以将li放到树的根(和其他一些地方)。然后我意识到可以将li拖到它自己的树的根上而不用也符合条件。

1 个答案:

答案 0 :(得分:1)

我在这里找不到问题和答案,一小时后,我自己解决了。

答案是,当拖动到树的根(或第二棵树)时,isAllowed:function(item,parent)会将parent设置为null,而不是你认为应该是root的ol或li !如此简单地添加一个检查可以解决这个问题。

以下是解决方案:

        isAllowed: function (item, parent) {
            if (parent == null) {
                return false; //Or do something else.
            }

            ...

此外,当我们开始使用nestedSortable时,connectWith和isAllowed花了我一天的时间,希望这也可以作为如何使用它们的示例。

相关问题