Components: Unable to delete a node [mat-tree component Angular Material 6.0.1]

Created on 31 May 2018  路  8Comments  路  Source: angular/components

insertNode is implemented but there's no method for deleting a node.
I tried removing the object from the array but it didn't work since the tree doesn't get updated.

Any ideas?

Most helpful comment

Hi, I create a parentMap to record each node's parent and delete node with it succesfully, here's the code

  • Declare the map
parentNodeMap = new Map<TreeNode, TreeNode>();
  • Set the map when inserting node
insertChild(parent: TreeNode) {
    const newChild = { name: '' } as TreeNode;
    if (parent.children) {
      parent.children.push(newChild);
      this.dataChange.next(this.data);
      this.parentNodeMap.set(newChild, parent);
    }
  }
  • Update the map after deleting
deleteNode(node: TreeNode) {
    const parentNode = this.parentNodeMap.get(node);
    const index = parentNode.children.indexOf(node);
    if (index !== -1) {
      parentNode.children.splice(index, 1);
      this.dataChange.next(this.data);
      this.parentNodeMap.delete(node);
    }
  }

hope this would be helpful.

All 8 comments

Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are stack overflow, gitter, mailing list, etc.

any thing new with this issue?

Hi, I create a parentMap to record each node's parent and delete node with it succesfully, here's the code

  • Declare the map
parentNodeMap = new Map<TreeNode, TreeNode>();
  • Set the map when inserting node
insertChild(parent: TreeNode) {
    const newChild = { name: '' } as TreeNode;
    if (parent.children) {
      parent.children.push(newChild);
      this.dataChange.next(this.data);
      this.parentNodeMap.set(newChild, parent);
    }
  }
  • Update the map after deleting
deleteNode(node: TreeNode) {
    const parentNode = this.parentNodeMap.get(node);
    const index = parentNode.children.indexOf(node);
    if (index !== -1) {
      parentNode.children.splice(index, 1);
      this.dataChange.next(this.data);
      this.parentNodeMap.delete(node);
    }
  }

hope this would be helpful.

@covera Thank you it seems great. could you please publish your code using https://stackblitz.com/
you can use angular-material examples to save time.

I am also facing the same issue,
I want to remove the leaf nodes on click of minus button.
I am using nested material tree
tree

Please if some on can help me with that
StackBlick Demo

Hi neerajrd25 ,

thanks for delete node demo.
but there is some error if we add more nodes and sub nodes.

Please inform me if you also facing same issue.

Hi @amarkoulavkar i have done it .. created stackblitz for it..
you can have a look
stackblitz

Since this is my business requirement, please bear some extra logic in it

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings