AngularMaterial表格中的renderRows()方法无法与matSort搭配使用。
创始人
2024-10-28 08:01:47
0

需要在使用 renderRows() 方法的时候手动重新排序表格。具体实现方法如下:

  1. 在 HTML 文件中,在 mat-table 标签上添加一个名为 "matSort" 的属性,并设置为 matSort 对象:

  1. 在 TypeScript 文件中,创建一个名为 "sort" 的 matSort 对象,并在 ngOnInit 函数中初始化它,并调用 renderRows() 方法:

import { MatSort } from "@angular/material/sort";

export class MyComponent implements OnInit { @ViewChild(MatSort) sort: MatSort;

ngOnInit(): void { this.dataSource.sort = this.sort; this.dataSource.sortingDataAccessor = (item, property) => { switch (property) { case "name": return item.name; case "age": return item.age; // ... default: return item[property]; } }; this.dataSource.sort.direction = "asc"; this.dataSource.sort.active = "name"; this.dataSource.sort.sortChange.subscribe(() => { this.dataSource.data = this.dataSource.data.slice(); // to trigger sorting }); this.dataSource.data = this.myDataService.getData(); // assuming a data service this.dataSource.sort.sort({id: "name", start: "asc"}); this.dataSource.paginator = this.paginator; this.dataSource.connect().subscribe(() => this.table.renderRows()); } }

  1. 在 ngOnInit 函数中,除了初始化 matSort 对象外,我们调用了两个方法来设置表格的排序方式:sortingDataAccessor 和 sort。sortingDataAccessor 是一个自定义函数,用于指定要排序的属性,因为 matSort 不支持嵌套属性。

  2. 我们还订阅了 sortChange 事件,以便可以手动触发 dataSource.data 的切片,从而重新排序数据。

  3. 最后,我们将 dataSource.data 连接到表格,以确保在排序或分页时刷新表格的显示。

这样,我们就可以在 Angular Material 表格中正确使用 renderRows() 方法和 matSort。

相关内容

热门资讯

透视推荐!aapoker透视怎... 透视推荐!aapoker透视怎么用(透视)智星德州插件2024最新版,教程法子(真的有挂)-哔哩哔哩...
透视普及!hhpoker透视码... 透视普及!hhpoker透视码(透视)aapoker公共底牌,教程模板(有挂规律)-哔哩哔哩1、上手...
透视揭露!hhpoker有没有... 透视揭露!hhpoker有没有开挂(透视)pokemmo辅助器手机版下载,教程步骤(有挂秘诀)-哔哩...
透视了解!hhpkoer辅助挂... 透视了解!hhpkoer辅助挂是真的吗(透视)哈糖大菠萝攻略,教程学习(证实有挂)-哔哩哔哩1、金币...
透视解迷!约局吧怎么看有没有挂... 透视解迷!约局吧怎么看有没有挂(透视)wepoker有没有机器人,教程窍门(确实有挂)-哔哩哔哩亲,...
透视推荐!wepokerplu... 透视推荐!wepokerplus透视脚本免费(透视)菠萝德州透视脚本,教程教材(存在有挂)-哔哩哔哩...
透视辅助!wepoker辅助器... 透视辅助!wepoker辅助器安装包(透视)wepoker插件程序激活码,教程阶段(有挂分析)-哔哩...
透视总结!wepoker安装教... 透视总结!wepoker安装教程(透视)约局吧开挂神器是真的吗,教程方式(有挂猫腻)-哔哩哔哩所有人...
透视详细!wepoker辅助器... 透视详细!wepoker辅助器安装包(透视)wepoker辅助器安装包,教程手筋(竟然有挂)-哔哩哔...
透视教你!wepoker透视有... 透视教你!wepoker透视有用吗(透视)智星菠萝透视,教程诀窍(有挂攻略)-哔哩哔哩1、首先打开w...