angular - MatTable 不刷新更新
问题描述
mat-table
我在对其数据执行更新操作后尝试刷新视图。但是,表视图仅在我第一次进行更新时正确刷新。每个后续更新操作都会延迟 1 个操作,如下所示:
- 更新第 1 行 -> 没有任何反应
- 更新第 2 行 -> 第 1 行得到更新
- 更新第 3 行 -> 第 2 行得到更新
等等...
该表使用 http.get 服务加载以获取数据。更新操作调用 http.post 服务来保存数据,随后调用该refreshData()
方法。两种服务都按预期工作。
我正在使用ChangeDetectorRef
强制更改检测。
零件:
dataSource: MatTableDataSource<IRegion> = new MatTableDataSource();
@ViewChild(MatSort) set content(sort: MatSort) {
this.dataSource.sort = sort;
};
constructor(private regionService: RegionService, private changeDetectorRefs: ChangeDetectorRef) {}
ngOnInit() {
this.populateRegions();
}
refreshData(){
this.populateRegions();
}
populateRegions(){
this.regionService.getRegions().subscribe({
next: regions => {
this.dataSource.data = regions;
this.changeDetectorRefs.detectChanges();
}
});
}
我在这里做错了什么?
解决方案
参考的使用RenderRows()
方法mat-table
:
@ViewChild(MatTable, {static: false}) table : MatTable
this.table.renderRows()
或者重新创建数据源对象:
populateRegions(){
this.regionService.getRegions().subscribe({
next: regions => {
this.dataSource = new MatTableDataSource(regions);
this.changeDetectorRefs.detectChanges();
}
});
}
推荐阅读
- kotlin - Kotlin yield 函数在取消协同程序时不合作
- javascript - 使用自动滑动创建图像滑块
- python - 我正在尝试使用 MATLAB 中的图像处理来分割光纤
- scroll - 无法在 MVC 中为车轮事件设置被动 false
- azure - 在 Azure 认知搜索中,如何匹配带有连字符的字段?
- arrays - 如何使用运行阶段声明的数组计算检查阶段的延迟?
- amazon-web-services - 注释“service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled”=“true”不适用于网络负载均衡器
- .htaccess - 重定向主域但保留子页面域
- php - 从ajax调用php方法,返回废话页面
- reactjs - 是的,接受两种情况作为 ip 地址和主机名