AngularMaterial@ViewChildMatTable:如何更新表格?
创始人
2024-10-28 08:01:15
0
  1. 首先,在组件的类中使用ViewChild来引用MatTable组件。 import { MatTable } from '@angular/material/table'; @ViewChild(MatTable) table: MatTable;

  2. 接下来,您可以通过调用MatTable的renderRows方法来强制组件重新渲染表格以更新数据。 this.table.renderRows();

下面是一个完整的示例代码: import { Component, OnInit, ViewChild } from '@angular/core'; import { MatTable } from '@angular/material/table';

@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'] }) export class MyComponentComponent implements OnInit { displayedColumns: string[] = ['position', 'name', 'weight', 'symbol']; dataSource = ELEMENT_DATA;

@ViewChild(MatTable) table: MatTable;

constructor() { }

ngOnInit(): void { }

updateTableData() { // Here you can update the data source of the table this.dataSource.push({position: 5, name: 'Hydrogen', weight: 1.0079, symbol: 'H'});

// This will render the updated data on the table
this.table.renderRows();

}

}

请注意,此示例代码假定您已经在组件中定义了要显示在表格中的列和数据。此外,当您更改数据源时,只需调用renderRows方法即可更新表格。

相关内容

热门资讯

分享实测!微乐自建房辅助工具脚... 分享实测!微乐自建房辅助工具脚本,微乐自建房脚本免费入口(开挂自建房)工具进入游戏-大厅左侧-新手福...
今日公布!微乐自建房辅助工具脚... 今日公布!微乐自建房辅助工具脚本,微乐家乡麻将自建房辅助app(开挂自建房)教程1)微乐家乡麻将自建...
总算了解!微乐小程序自建房辅助... 总算了解!微乐小程序自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)插件1、微乐小程序自建房辅...
玩家必看秘籍!微乐小程序自建房... 玩家必看秘籍!微乐小程序自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具;1、首先...
重大推荐!微乐小程序麻将自建房... 重大推荐!微乐小程序麻将自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、微乐小...
必看攻略!微乐微信自建房辅助工... 必看攻略!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)软件1、下载好微乐微信自建房...
盘点一款!微信雀神麻将挂件开挂... 盘点一款!微信雀神麻将挂件开挂辅助,微乐小程序免费黑科技(开挂自建房)器1、游戏颠覆性的策略玩法,独...
玩家必看科普!微信卡五星辅助器... 玩家必看科普!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)工具1、玩家必看科普!微信卡五星...
交流学习经验!微乐自建房辅助工... 交流学习经验!微乐自建房辅助工具挂,微乐小程序免费黑科技(开挂自建房)工具1、微乐自建房辅助工具挂模...
最新研发!微乐自建房辅助工具脚... 最新研发!微乐自建房辅助工具脚本,微乐自建房脚本免费入口(开挂自建房)工具微乐自建房辅助工具脚本透视...