我有一个带有排序和分页器的垫表。我的组件看起来像这样,因为在 ngOnInit 中我注册到一个调用 populateAlertTableSource 函数的事件处理程序。调用此函数时,表格中的数据确实会刷新,但绘制器不会,除非我将鼠标悬停在表格上(或单击分页器)。当我从表中删除一行时也会发生这种情况 - 在我将鼠标悬停在表上(或单击分页器)之前,该行不会消失并且分页器不会刷新。当我在其声明中手动填充数据源时,这似乎不会发生,并且 pagiantor 会当场更新。
export class SomeComponent implements OnInit {
AlertsCollection: any = [];
alertsDataSource = new MatTableDataSource(this.AlertsCollection);
@ViewChild('AlertsMatSort') alertSort: MatSort;
@ViewChild("AlertsPaginator") paginator: MatPaginator;
constructor(private alertsService: AlertsService, private alertsHubService: AlertsHubService,
private changeDetectorRefs: ChangeDetectorRef, private modalService: NgbModal) {}
ngOnInit() {
this.alertsHubService.subscribe(utils.url_root);
this.alertNotificationProxy = this.alertsHubService.generateProxy();
this.alertsService.registerProxy(this.alertNotificationProxy, 'updateDashboard', (message: any[]) => {
this.populateAlertTableSource(message)
});
}
ngAfterViewInit() {
this.alertsDataSource.paginator = this.paginator;
this.alertsDataSource.sort = this.alertSort;
}
populateAlertTableSource(message: any) {
this.AlertsCollection = message
this.alertsDataSource.data = this.AlertsCollection;
this.alertsDataSource.paginator = this.paginator;
this.alertsDataSource.sort = this.alertSort;
this.alertsTable.renderRows();
this.refresh();
}
deleteAlert(alert: any) {
this.alertsService.skipAlert(alert).subscribe(() => {
var index = this.AlertsCollection.indexOf(alert, 0);
if (index > -1)
this.AlertsCollection.splice(index, 1);
this.alertsDataSource.data = this.AlertsCollection;
this.alertsTable.renderRows();
},
(error: AppError) => {
console.log(error);
throw error;
}
);
}
refresh() {
this.changeDetectorRefs.detectChanges();
}
}