我正在尝试获取表的实例,以便在更新数据源时使用 renderrows() 函数。
我已经尝试过使用@ViewChild 来完成它,但无论我做什么它都是未定义的。
组件.ts:
import {
MatTable,
MatTableDataSource,
MatPaginator,
MatSelectModule
} from "@angular/material";
@ViewChild(MatTable, { static: true }) playersTable: MatTable<any>;
addToDataSource(data) {
for (let index = 0; index < data.length; index++) {
this.dataSource.data.push(data[index]);
}
this.playersTable.renderRows(); // this.playersTable is undefined.
}
.html:
<div class="mat-elevation-z8">
<table
mat-table
#playersTable
[dataSource]="dataSource"
*ngIf="!loadingData; else loading"
class="row"
>
...
</table>