Html 如何获取mat表的实例?
我试图获取表的实例,以便在更新数据源时使用renderrows()函数 我曾经尝试过使用@ViewChild来完成这项工作,但无论我做什么,它都是未定义的 组件1.ts:Html 如何获取mat表的实例?,html,angular,angular-material,angular-material-table,Html,Angular,Angular Material,Angular Material Table,我试图获取表的实例,以便在更新数据源时使用renderrows()函数 我曾经尝试过使用@ViewChild来完成这项工作,但无论我做什么,它都是未定义的 组件1.ts: import { MatTable, MatTableDataSource, MatPaginator, MatSelectModule } from "@angular/material"; @ViewChild(MatTable, { static: true }) playersTable: MatTa
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.
}
导入{
马特布尔,
MatTableDataSource,
MatPaginator,
Matselect模块
}从“@角度/材料”;
@ViewChild(MatTable,{static:true})playersTable:MatTable;
addToDataSource(数据){
for(让index=0;index
.html:
<div class="mat-elevation-z8">
<table
mat-table
#playersTable
[dataSource]="dataSource"
*ngIf="!loadingData; else loading"
class="row"
>
...
</table>
...
为您的垫子桌提供Id
<table mat-table #playersTable[dataSource]="dataSource">
然后使用ViewChild可以访问表的实例。
使用新的MatTableDataSource()分配数据,而不是将数据推送到数据源
@ViewChild('playersTable',{static:true})playersMatTable:MatTable;
addToDataSource(数据){
this.dataSource=新MatTableDataSource(数据);
this.playersTable.renderRows();
}
或者,如果要向现有数据源添加数据,则需要在不使用实例的情况下刷新数据源
addToDataSource(data) {
for (let index = 0; index < data.length; index++) {
this.dataSource.data.push(data[index]);
}
this.dataSource = [...this.dataSource]; //refresh the dataSource
}
addToDataSource(数据){
for(让index=0;index
为您的垫子桌提供Id
<table mat-table #playersTable[dataSource]="dataSource">
然后使用ViewChild可以访问表的实例。
使用新的MatTableDataSource()分配数据,而不是将数据推送到数据源
@ViewChild('playersTable',{static:true})playersMatTable:MatTable;
addToDataSource(数据){
this.dataSource=新MatTableDataSource(数据);
this.playersTable.renderRows();
}
或者,如果要向现有数据源添加数据,则需要在不使用实例的情况下刷新数据源
addToDataSource(data) {
for (let index = 0; index < data.length; index++) {
this.dataSource.data.push(data[index]);
}
this.dataSource = [...this.dataSource]; //refresh the dataSource
}
addToDataSource(数据){
for(让index=0;index
static:true
->static:false
如果不起作用,请尝试@ViewChild('playersTable',…
更改static属性不起作用,我不确定您所说的“@ViewChild('playersTable',…”是什么意思。我需要给ViewChild一个模块来使用(比如MatTable,MatPaginator)通过传递字符串,您引用了hashtag,得到了它,但遗憾的是仍然没有定义…static:true
->static:false
如果不起作用,请尝试@ViewChild('playersTable',…
更改static属性不起作用,我不确定您所说的“@ViewChild”是什么意思('PlayerTable',…',我需要给ViewChild一个模块来使用(比如MatTable,MatPaginator)通过传递一个字符串,您引用了hashtag,得到了它,但遗憾的是仍然没有定义…像您所说的那样刷新数据源工作得很好,确实解决了我的问题。尽管我仍然不知道为什么我无法获取表本身的引用。谢谢!正如我在第一种方法中所说的,如果您使用新关键字更新数据源,那么您将获得表ref如果我给出一个数组(嵌套数组),该怎么办动态更新数据源,那么我们如何进行刷新。请告诉我,如果您知道,像您所说的那样刷新数据源非常有效,并且确实解决了我的问题。尽管我仍然不知道为什么我无法获取表本身的引用。谢谢!正如我在第一种方法中所说的,如果您使用新关键字更新数据源,那么您将获得表r若我动态地给数据源一个数组(嵌套数组),那个么我们如何刷新呢?若你们知道,请告诉我