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

相关内容

热门资讯

透视了解"大菠萝手游... 透视了解"大菠萝手游辅助"果然是有辅助攻略(哔哩哔哩)1、超多福利:超高返利,海量正版游戏,大菠萝手...
透视必备"aapok... 透视必备"aapoker透视脚本入口"好像是真的辅助工具(哔哩哔哩)1、aapoker透视脚本入口模...
透视透视"wepok... 透视透视"wepoker辅助器安装包"总是存在有辅助插件(哔哩哔哩)1、许多玩家不知道wepoker...
透视开挂"wpk俱乐... 透视开挂"wpk俱乐部是真的吗"果然是真的辅助教程(哔哩哔哩)1、首先打开wpk俱乐部是真的吗辅助器...
透视推荐"pokem... 透视推荐"pokemmo辅助官网"总是真的是有辅助神器(哔哩哔哩)1、很好的工具软件,可以解锁游戏的...
透视解谜"德州透视插... 透视解谜"德州透视插件"总是是真的辅助器(哔哩哔哩)德州透视插件脚本下载中分为三种模型:德州透视插件...
透视普及"德普之星有... 透视普及"德普之星有透视辅助吗"一直存在有辅助攻略(哔哩哔哩)1、操作简单,无需德普之星有透视辅助吗...
透视详细"wepok... 透视详细"wepoker透视有没有"确实真的是有辅助攻略(哔哩哔哩)1、许多玩家不知道wepoker...
透视专业"wepok... 透视专业"wepoker私人局有透视吗"其实真的有辅助教程(哔哩哔哩)1.wepoker私人局有透视...
透视揭幕"德州之星扫... 透视揭幕"德州之星扫描器"原来是真的辅助挂(哔哩哔哩)1、金币登录送、破产送、升级送、活动送。详细领...