1

This code will work to create a nested tree structure. I am also able to add sub node. I am stuck to delete current node and its children. How to delete the current node and its children on clicking remove button also how to add a new node at the same level

    @Component({
          selector: 'tree-node'
          template: `
          <div>{{node.name}} <button(click)="addNewNode(node)">Add</button> <button (click)="removeNode(node)">Remove</button></div>
          <ul>
            <li *ngFor="let node of node.children">
              <tree-node  [node]="node"></tree-node>
            </li>
          </ul>
        `})

  export class TreeNode {
          @Input() node;
    addNewNode(node) {
        const newNode = {name: 'newNode', children: []};
        node.children.push(newNode);

   }

    removeNode(node) {
    node.children = [] // this code will remove only its child node
    }
}
         @Component({
          selector: 'my-app',
          providers: [],
          template: `
            <div>
              <h2>Hello {{name}}</h2>
              <tree-node [node]="node"></tree-node>
            </div>
          `,
          directives: [TreeNode]
        })
        export class App {
          constructor() {
            this.name = 'Angular2 (Release Candidate!)'
          }

          node = {name: 'root', children: [
            {name: 'a', children: []},
            {name: 'b', children: []},
            {name: 'c', children: [
              {name: 'd', children: []},
              {name: 'e', children: []},
              {name: 'f', children: []},
             ]},
          ]};  
        }

1 Answer 1

0

You can find Add, Remove and update operation in Angular 6 material nested tree Angular 6 Material Nested Tree is not working with dynamic data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.