0

I have a question about tree component. Is it possible to make my tree component like a table ? Here is what i exactly want:

enter image description here

Here is some snippet from example of documentation:

nodes = [
    {
      id: 1,
      name: "Санхүү",
      children: [{ id: 2, name: "child1" }, { id: 3, name: "child2" }]
    },
    {
      id: 4,
      name: "Санхүүгийн бус",
      children: [
        { id: 5, name: "Данс нээх харилцах" },
        {
          id: 6,
          name: "Карт захиалах",
          children: [{ id: 7, name: "subsub" }]
        }
      ]
    }
  ];
batgerel.e
  • 837
  • 1
  • 10
  • 31

1 Answers1

1

I think that the best aproach is create a table with the nodes. For this we use the variable "data" and a recursive function.

data:any[]=[]
  getData(data:any[],index,nodes)
  {
    nodes.forEach(x=>{
      x.level=index
      data.push(x)
      if (x.children && x.open)
        this.getData(data,index+1,x.children)
    })
    return data;
  }

See that in "data" we has the nodes and a "level" to allow us to know in wich level are our node. If we has an .html like

<table>
  <tr *ngFor="let item of data">
    <td [style.padding-left]="(2*item.level)+'rem'">
      <button *ngIf="item.children" (click)="click(item)">+</button>
      {{item.name}}</td>
  </tr>
</table>

When we make a click we call to the function

click(item)
  {
    item.open=!item.open;
    this.data=[];
    this.getData(this.data,0,this.nodes)
  }

Really is an ugly example, but this stackblitz show the table

Eliseo
  • 50,109
  • 4
  • 29
  • 67
  • if i want data without render ? – batgerel.e Nov 12 '19 at 01:52
  • Sorry, I don't understant you. the "data" exist after you call to `getData(this.data,0,this.nodes)`. You can show or not. if you want to say that how get all nodes, simply remove the condition `&& x.open` – Eliseo Nov 12 '19 at 07:24