0

I implement a tree stucture for my UI with material tree, in which a new Object for this tree will be needed. this object looks like this:

[
  {
    name: 'Fruit',
    children: [
      {name: 'Apple'},
      {name: 'Banana'},
      {name: 'Fruit loops'},
    ]
  }
]

but now what I have looks like this:

[
 {"contact" : 
           { information: {name: "abc", address:"asdfa"},
           type: "phone"
           value: "123212123"

}
]

expected output object looks like this:

[
  {
    name: 'contact',
    children: [

 {name: "infmormation",
       children: [
           {name: "address",
       children: [
          {name: 'absdsdc'},
    
        ]},
          {name: "name",
       children: [
          {name: 'adasdf'},
    
        ]},
    
        ]},
 
      {name: "type",
       children: [
          {name: 'abc'},
    
        ]},
          {name: "type",
       children: [
          {name: 'Broccoli'},
    
        ]},
      
    ]
  }

]

how I can create a tree object for my input object with a dynamic funcktion how create a recusive method, which can create dynmiac output method. I need your help. any solution?

user1938143
  • 1,022
  • 2
  • 22
  • 46

1 Answers1

0

In html

<mat-tree [dataSource]="dataSource" [treeControl]="treeControl" >
    <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle matTreeNodePadding>
      <span>{{node.name}}</span>
      [...]
    </mat-tree-node>
</mat-tree>

In Ts

//Variables tree
treeControl: FlatTreeControl<FileFlatNode>;
treeFlattener: MatTreeFlattener<YourObject, FileFlatNode>;
dataSource: MatTreeFlatDataSource<YourObject, FileFlatNode>;
transformer = (node: YourObject, level: number) => {
    return new FileFlatNode(node.name,node.expandable,node.level,node.childrens);
}

// Functions tree
private _getLevel = (node: FileFlatNode) => node.level;
private _isExpandable = (node: FileFlatNode) => node.expandable;
private _getChildren = (node: YourObject): Observable<YourObject[]> => 
observableOf(node.childrens);

//Inherit class represent tree node
export class FileFlatNode {
  constructor(public name: string,
              public expandable: boolean,
              public level: number,
              public childrens: YourObject[]) {
     //Here you can do the mapping between your object and the tree node
  }
}

// In constructor of main class
this.treeFlattener = new MatTreeFlattener(this.transformer, this._getLevel,
this._isExpandable, this._getChildren);
JLazar0
  • 1,257
  • 1
  • 11
  • 22
  • the problem, how can I create tree object dynamically – user1938143 Aug 03 '20 at 14:16
  • Excuse me, I don't understand the question well, to create the tree object model dynamically, or to create the nodes dynamically? – JLazar0 Aug 03 '20 at 19:17
  • I want to create tree object model dynamically. – user1938143 Aug 04 '20 at 13:56
  • the only thing I can think of is to combine the innerHtml property inside a node with the following link https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object – JLazar0 Aug 04 '20 at 14:51