Copy selected nodes to a new tree

1k views Asked by At

I'm trying to copy all selected nodes from one fancytree control to another one on the same page. So far I've tried the following code but the second tree remains blank:

        var sourceTree= $("#tree").fancytree("getTree");
        var destinationTree= $("#destinationTree").fancytree("getTree");

        var selectedNodes = sourceTree.getSelectedNodes();
        var rootNode = destinationTree.rootNode;

        rootNode.addChildren(selectedNodes);

Any ideas?

Thanks

1

There are 1 answers

1
mar10 On BEST ANSWER

addChildren expects a plain object, so you could try

$.each(sourceTree.getSelectedNodes(), function(idx, node){
    destinationTree.rootNode.addNode(node.toDict());
});

or

$.each(sourceTree.getSelectedNodes(), function(idx, node){
    node.copyTo(destinationTree.rootNode);
});