ng2-tree
ng2-tree copied to clipboard
Turning a leaf into a node
Hi all!
First of all lemme thank you for this cool lib!
I was wondering about a use case that we need for an app we're working on: turning a leaf into a node. Is there an easy way to do it? Currently I hacked the solution by using a custom action:
const controller = this.customTree.getControllerByNodeId(e.node.id);
controller.setChildren([]);
controller.addChild({id: 'newToBeRemoved', value: ''});
setTimeout(() => this.customTree.getControllerByNodeId('newToBeRemoved').remove());
However this is ugly and requires modifying the TreeController
API in order to allow setting children even if the tree is a leaf.
Could you point me in some direction? Thanks!
[edit]: I also managed to achieve it without meddling with the library sources:
e.node.removeItselfFromParent();
e.node.node.children = [];
e.node.node._foldingType = FoldingType.Empty;
e.node.parent.addChild(new Tree(e.node.node));
this keeps the original order of the elements in tree:
e.node.node.children = [];
e.node.node._foldingType = FoldingType.Expanded;
const children = e.node.parent.children;
for (let i = 0; i < children.length; i++) {
if (children[i].id === e.node.node.id) {
children[i].node.children = [];
children[i].node._foldingType = FoldingType.Empty;
break;
}
}
e.node.parent.setChildren(children.map(ch => ch.node));
still I'd appreciate to hear someone's opinion on this topic