材料表:将数据源初始化放置在哪里,以保留带有路由器出口的组件状态

问题描述 投票:0回答:1

我正在将此Angular 8和材料数据表与不同的过滤器一起使用,如此example将此组件放置在一个组件中,我可以通过它在路由器出口中导航,只有一个细微的差别,我在ngAfterViewInit中初始化dataSource,并在该块的末尾将API中的数据分配给this.dataSource.data以实现最快的性能,如建议的[C0 ]

here

问题是,当我通过数据表过滤组件中的某些内容,然后每次回到具有数据表的组件时,在不同的路由/组件之间切换时,由于重新初始化,我看不到过滤结果在路线之间切换时。我确认ngAfterViewInit在此类事件中被调用。在路由之间切换时,能否以某种方式保留组件dataTable的状态?

angular angular-material angular-components router-outlet material-table
1个回答
0
投票
我从Stackoverflow ngAfterViewInit() { this.dataSource.sort = this.sort; this.dataSource.paginator = this.paginator; this.dataSource.filterPredicate = (record, filter: any) => { let result = true; let keys = Object.keys(record); // keys of the object data for (const key of keys) { let searchCondition = filter.conditions[key]; // get search filter method if (searchCondition && searchCondition !== 'none') { if (filter.methods[searchCondition](record[key], filter.values[key]) === false) { // invoke search filter result = false // if one of the filters method not succeed the row will be remove from the filter result break; } } } return result }; this.apiService.getLogsNested().subscribe(logsFromApi => { this.dataSource.data = logsFromApi; this.isLoadingResults = false; }); console.log('logs ngAfterViewInit'); } 中的可接受答案中使用RouteReuseStrategy实现了解决方案它解决了我的问题。
© www.soinside.com 2019 - 2024. All rights reserved.