From a8e804d1cf82dab79bfd1ee5d27a9f1973df341d Mon Sep 17 00:00:00 2001 From: Stromwerk Date: Tue, 21 Jan 2020 03:00:26 +0200 Subject: [PATCH] Fix #24 Table not updating after tree has changed This commit fixes the issue of the table not updating as the tree passed to it changed. This is done by implementing OnChanges and rerunning initialization code after the tree has changed --- .../component/treetable.component.ts | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) 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'); }