diff --git a/src/app/treetable/component/treetable.component.ts b/src/app/treetable/component/treetable.component.ts index a5258ff..c9cd15c 100644 --- a/src/app/treetable/component/treetable.component.ts +++ b/src/app/treetable/component/treetable.component.ts @@ -1,4 +1,12 @@ -import { Component, OnInit, Input, Output, ElementRef } from '@angular/core'; +import { + Component, + OnInit, + Input, + Output, + ElementRef, + OnChanges, + SimpleChanges, +} from '@angular/core'; import { Node, TreeTableNode, Options, SearchableNode } from '../models'; import { TreeService } from '../services/tree/tree.service'; import { MatTableDataSource } from '@angular/material'; @@ -14,7 +22,7 @@ import { Subject } from 'rxjs'; templateUrl: './treetable.component.html', styleUrls: ['./treetable.component.scss'] }) -export class TreetableComponent implements OnInit { +export class TreetableComponent implements OnInit, OnChanges { @Input() @Required tree: Node | Node[]; @Input() options: Options = {}; @Output() nodeClicked: Subject> = new Subject(); @@ -53,6 +61,17 @@ export class TreetableComponent implements OnInit { this.dataSource = this.generateDataSource(); } + ngOnChanges(changes: SimpleChanges) { + if (changes.tree.isFirstChange()) { + return; + } + this.tree = Array.isArray(this.tree) ? this.tree : [this.tree]; + this.searchableTree = this.tree.map(t => this.converterService.toSearchableTree(t)); + const treeTableTree = this.searchableTree.map(st => this.converterService.toTreeTableTree(st)); + this.treeTable = flatMap(treeTableTree, this.treeService.flatten); + this.dataSource = this.generateDataSource(); + } + extractNodeProps(tree: Node & { value: { [k: string]: any } }): string[] { return Object.keys(tree.value).filter(x => typeof tree.value[x] !== 'object'); }