Angular2 UL/LI JSON-tree recursive in ngFor
Asked Answered
M

2

28

I'd like to convert JSON-trees into unordered lists in Angular2. I know the recursive directive solution from Angular1 and I am pretty sure the solution in Angular2 will be recursive too.

    [
        {name:"parent1", subnodes:[]},
        {name:"parent2", 
            subnodes:[
                    {name:"parent2_child1", subnodes:[]}
                 ],
        {name:"parent3", 
            subnodes:[
                    {name:"parent3_child1", 
                        subnodes:[
                                {name:"parent3_child1_child1", subnodes:[]}
                             ]
                    }
                 ]
        }
    ]

to this unordered list

<ul>
    <li>parent1</li>
    <li>parent2
        <ul>
            <li>parent2_child1</li>
        </ul>
    </li>
    <li>parent3
        <ul>
            <li>parent3_child1
                <ul>
                    <li>parent3_child1_child1</li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

using Angular2 and ngFor. Anyone got an idea?

Mahound answered 1/3, 2016 at 20:49 Comment(2)
#35647865 shows how to iterate over JSON. I guess you need a component wrapping the ngFor to be able to use it recursive.Asia
There is a tree component in PrimeNG you can check, it uses another component called p:treeNode internally that does the recursion. primefaces.org/primeng/#/treeVanwinkle
T
34

Borrowing from Torgeir Helgevold's post, I came up with this Plunkr. Here's the code:

TreeView Component:

import {Component, Input} from 'angular2/core';

@Component ({
  selector: 'tree-view',
  directives: [TreeView],
  template: `
  <ul>
    <li *ngFor="#node of treeData">
      {{node.name}}
      <tree-view [treeData]="node.subnodes"></tree-view>
    </li>
  </ul>
  `
})
export class TreeView {
  @Input() treeData: [];
}

App Component:

import {Component} from 'angular2/core';
import {TreeView} from './tree-view.component';

@Component({
    selector: 'my-app',
    directives: [TreeView],
    template: `
    <h1>Tree as UL</h1>
    <tree-view [treeData]="myTree"></tree-view>
    `
})
export class AppComponent { 
  myTree =     [
        {name:"parent1", subnodes:[]},
        {name:"parent2", 
            subnodes:[
                    {name:"parent2_child1", subnodes:[]}
                 ],
        {name:"parent3", 
            subnodes:[
                    {name:"parent3_child1", 
                        subnodes:[
                                {name:"parent3_child1_child1", subnodes:[]}
                             ]
                    }
                 ]
        }
    ];
}
Thuggee answered 20/4, 2016 at 19:6 Comment(0)
M
47

You don't need to make a new tree-view component to do this, you can simply use this pattern in any of your templates:

If your data array was public property list of your component:

<h1>Angular 2 Recursive List</h1>
<ul>
  <ng-template #recursiveList let-list>
    <li *ngFor="let item of list">
      {{item.name}}
      <ul *ngIf="item.children.length > 0">  <!-- item.subnodes.length -->
        <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.children }"></ng-container>
      </ul>
    </li>
  </ng-template>
  <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: list }"></ng-container>
</ul>

Here's a gist.

Michaels answered 7/5, 2017 at 20:25 Comment(6)
Very similar to the angular 1 recursive template solution. Nice and clearIrving
There's a way to use a filter in this recursive template?Apparatus
What do you mean by filter? You can use a *ngIf="filter" on any of the elements including the <ng-container>.Michaels
This should be the accepted answer, much better when we have a more complicated view, thank you!Toback
Did not work for me, using angular 6, copy pasted code. Only parent1 parent2, parent3 are displayed.Delicacy
Can you please create table headers using the same json having nested data like row 1 spaning first level of json , row 2 having secong level of json and row3 having third nested level and so onEarthbound
T
34

Borrowing from Torgeir Helgevold's post, I came up with this Plunkr. Here's the code:

TreeView Component:

import {Component, Input} from 'angular2/core';

@Component ({
  selector: 'tree-view',
  directives: [TreeView],
  template: `
  <ul>
    <li *ngFor="#node of treeData">
      {{node.name}}
      <tree-view [treeData]="node.subnodes"></tree-view>
    </li>
  </ul>
  `
})
export class TreeView {
  @Input() treeData: [];
}

App Component:

import {Component} from 'angular2/core';
import {TreeView} from './tree-view.component';

@Component({
    selector: 'my-app',
    directives: [TreeView],
    template: `
    <h1>Tree as UL</h1>
    <tree-view [treeData]="myTree"></tree-view>
    `
})
export class AppComponent { 
  myTree =     [
        {name:"parent1", subnodes:[]},
        {name:"parent2", 
            subnodes:[
                    {name:"parent2_child1", subnodes:[]}
                 ],
        {name:"parent3", 
            subnodes:[
                    {name:"parent3_child1", 
                        subnodes:[
                                {name:"parent3_child1_child1", subnodes:[]}
                             ]
                    }
                 ]
        }
    ];
}
Thuggee answered 20/4, 2016 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.