Angular 4 PrimeNg 树拖放复制到

Angular 4 PrimeNg Tree Drag and Drop Copy to

几天来我一直在使用 PrimeNg 的 Tree 模块,并且我一直在为我想让它做的几乎所有事情编写自定义功能。但是,由于回调被调用 post-manipulation.

,我的自定义 copyTo 函数中的对象引用仍然存在问题

所以,我正在寻找一种优雅的方式来中断拖放处理程序,以便我可以深入复制数据以实现更精细的控制。我觉得我应该考虑扩展 class 以便我可以覆盖插件的功能,但我不确定这是否会在升级时造成更多伤害。

非常感谢任何意见或"How To"建议!

下面的代码获取一个具有已知索引的元素,并将其重新插入到从中拖动它的树中,从而从一棵树复制到另一棵树。但是,这仅适用于已知索引,不支持不同深度的嵌套节点。

    copyToTree: any = []; // tree obj cache
    staticTree: any = []; // tree obj cache

    // Fires when node is dropped on copyToTree
    dropOnCopyTo(event: any) {
        let dragged: any = event.dragNode;

        if(dragged.field_name == null) { // dragged from staticTree
            // re-add copy to correct position in staticTree
            let copy: any = this.deepCopy(dragged);
            this.staticTree.children.splice(copy.index, 0, copy);
            // Update field_name of dragged in copyToTree
            dragged.field_name = 'new_' + Date.now();
        }
    }

在与另一位开发人员合作后,我们发现我们只需要一个适当的深层副本来处理循环引用,因为 TreeNode.parent.children 数组包含原始 TreeNode 对象的引用。

public deepCopy(oldObj: any) {
    // Using jquery for its extensive error checking
    return $.extend(true, {}, oldObj);
}

这允许将 TreeNode 拼接回 "from" 树,而无需绑定对象引用。