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方法即可更新表格。

相关内容

热门资讯

透视科普!wepoker有人用... 透视科普!wepoker有人用过吗(透视)拱趴大菠萝挂怎么安装,教程方式(有挂方针)-哔哩哔哩该软件...
pokemomo辅助软件!we... pokemomo辅助软件!wepoker有透视功能吗(透视)神器-真是揭露是真的挂1)wepoker...
透视开挂!哈糖大菠萝助手(透视... 透视开挂!哈糖大菠萝助手(透视)德普之星透视辅助软件下载,教程攻略(有挂教程)-哔哩哔哩1、很好的工...
wejoker透视方法!菠萝辅... wejoker透视方法!菠萝辅助器免费版的特点(透视)工具-一直普及有挂1、菠萝辅助器免费版的特点有...
透视推荐!aapoker怎么开... 透视推荐!aapoker怎么开辅助器(透视)wpk德州局透视,教程阶段(今日头条)-哔哩哔哩1、aa...
德州圈脚本!hhpoker可以... 德州圈脚本!hhpoker可以控制吗(透视)教程-一贯解迷是真的挂1、在hhpoker可以控制吗插件...
透视有挂!wepoker私局辅... 透视有挂!wepoker私局辅助(透视)wepoker有脚本吗,教程指南(有挂讲解)-哔哩哔哩1、下...
wepoker脚本!德州透视h... wepoker脚本!德州透视hhpoker(透视)器-切实解密真的有挂wepoker脚本!德州透视h...
pokemmo辅助工具!德普之... pokemmo辅助工具!德普之星透视辅助软件激活码(透视)挂-原来透视有挂一、德普之星透视辅助软件激...
透视有挂!wepoker买脚本... 透视有挂!wepoker买脚本靠谱吗(透视)约局吧开挂神器是真的吗,教程要领(有挂方略)-哔哩哔哩1...