Reputation: 11
Can someone tell me how to remove a specific node from my tree ?
Example: root node(this.documentsTree[0]) children(this.documentsTree[0].children[0], this.documentsTree[0].children[1]). The first child also have a child(this.documentsTree[0].children[0].children[0]) <= This is just a small example. I'm working with Angular and I'm using Tree from PrimeNG. Below you can see some part of my code. Thanks!:
import {TreeModule} from 'primeng/tree';
import {TreeNode} from 'primeng/api';
export class DocumentsComponent implements OnInit {
documentsTree: TreeNode[] = [];
private createNode(category: CategoryModel) {
let node = {
data: category,
label: category.name,
expandedIcon: "fa-folder-open",
collapsedIcon: "fa-folder",
expanded: category.id == 1,
children: [],
leaf: false
};
category.children.forEach(x => node.children.push(this.createNode(x)));
return node;
}
private loadCategoryTree() {
this.documentsTree = [];
var node = this.createNode(this.documentCategoryDTO);
this.documentsTree = [node];
this.documentsTreeAux = this.documentsTree;
}
Upvotes: 1
Views: 4318
Reputation: 81
Here is the method I used for deleting from a primeng tree adapted to your example, supposing your node data has a unique identifier (ex. ID).
deleteNode(topNode: TreeNode, selectedNode: TreeNode) {
if (topNode.children != null) {
var i;
for (i = 0; i < topNode.children.length; i++) {
if (topNode.children[i].data.id == selectedNode.data.id) {
topNode.children.splice(i, 1);
return;
}
else this.deleteNode(topNode.children[i], selectedNode);
}
}
else return;
}
And you can use it like this:this.deleteNode(this.documentsTree[0], selectedNode);
Hope this helps.
Upvotes: 8