通缉:ExtJS中自定义树到树拖放实现的方向/想法

时间:2010-08-18 07:58:00

标签: drag-and-drop extjs tree

我需要一些关于 ExtJS 中两棵树之间拖拽的组合功能。

第一个必需的功能非常简单,只是内置的拖放功能仅隔离到单个

Standard tree drag'n'drop

第二个必需的功能是我不希望用户能够从左树拖动节点并将其放在右树。

One way non destructive drag'n'drop

操作不应该从左树中删除节点,因此可以将同一节点从左树拖到中的多个位置右树

我的问题是:我应该采用哪种方法来结合这两个功能,利用TreePanel对象中的现有可能性而不再发明轮子?我不是在寻找一个完整的解决方案(虽然它会很好;-)),而是如何处理拖放区域,事件等等。

1 个答案:

答案 0 :(得分:3)

好。我一直在考虑这个问题,以下方法似乎对我有用:)

我已将树配置为:

listeners:
{
    beforenodedrop: function (dropEvent) {
        // Does this node come from the right tree?
        if (dropEvent.source.tree.id !== dropEvent.tree.id) {
            // The node should be discarded.
            dropEvent.dropNode.parentNode.removeChild(dropEvent.dropNode, true);

            // The node has been discarded, return drop succeeded.
            dropEvent.dropStatus = true;
            return false;
        }
        return true;
    },  
    nodedragover: function (dragevent) {
        // If the node comes from the right tree, it is allowed to be dropped here.
        if (dragevent.source.tree.id !== dragevent.tree.id) {
            return true;
        }
        // A node from this tree is not allowed to be dropped.
        return false;
    }
}

树的配置如下:

listeners:
{   
    beforenodedrop: function (dropEvent) {
        // Does this node come from the left tree?
        if (dropEvent.source.tree.id !== dropEvent.tree.id) {
            // The node should be cloned and inserted in the right tree.

            // Copy the node.
            var node = dropEvent.dropNode; // the node that was dropped
            var nodeCopy = new Ext.tree.TreeNode( // copy it
                Ext.apply({}, node.attributes)
            );
            // Match the id's.
            nodeCopy.id = Ext.id(null,'newnode') + '_' + node.id;

            // Find the right place to put it.
            if (dropEvent.target.parentNode === dropEvent.tree.getRootNode()) {
                // The node is placed on a folder, thus drop it there.
                dropEvent.target.appendChild(nodeCopy);
            } else {
                // The node is placed inside a folder, thus place it in there.
                dropEvent.target.parentNode.appendChild(nodeCopy);
            }

            // The node has been dropped, return okay and stop further process.
            dropEvent.dropStatus = true;
            return false;
        }           
        // Just use the normal builtin drag and drop.
        return true;
    }
}

两棵树都设置为启用Drag'n'Drop:

enableDD: true

所有叶节点都具有以下配置:

allowDrop: true,
draggable: true

所有文件夹都设置为:

allowDrop: true,
draggable: false

结论是,我选择覆盖 treepanel 中的一些内置拖放方法,同时仍保留内置功能。<​​/ p>