Sophia
Sophia

Reputation: 21

TreeNode - PrimeNG: How to Remove a node from tree?

enter image description here

I have a tree and if I select two nodes they are saved correctly, but if I later decide to delete only one node of the two previously selected, both are deleted. How can I delete only one? this is the part of the code where I check for unselected nodes. The problem is the delete, but I don't know how to do it. Consider that the selected nodes are passed into a map -> functions <with key / value>

nodeUnselect(event) {
    if (this.dialogDate) {
      this.selectedNode.push(event.node);
      if (event.node.parent) {
        this.selectedNode.push(event.node.parent);
      }
    } else {
      if (event.node.parent) {
        if (this.userData.functions && event.node.data && event.node.data.functionFK) {
          delete this.userData.functions[event.node.data.functionFK];
        }
      } else {
        if (event.node.children && event.node.children.length) {
          for (const childNode of event.node.children) {
            if (this.userData.functions && childNode.data && childNode.data.functionFK) {
              delete this.userData.functions[childNode.data.functionFK];
            }
          }
        }
      }
    }
  }

If you need more code I'll post it

Upvotes: 1

Views: 863

Answers (1)

Kamran Sohail
Kamran Sohail

Reputation: 632

Is may help some one,

deleteNode(node:any) : void {
    if (node.parent != null) {
      console.log(node.buildingId)
      console.log(node.parent.children.indexOf(node))
      node.parent.children.splice(node.parent.children.indexOf(node), 1)  
      
    }}

Upvotes: 0

Related Questions