Dr. Greenthumb
Dr. Greenthumb

Reputation: 2316

Copy selected nodes to a new tree

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

Upvotes: 1

Views: 1119

Answers (1)

mar10
mar10

Reputation: 14794

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);
});

Upvotes: 4

Related Questions