Angular Material:如何基于单一数据源通过服务展示多组不同数据的MatTable
实现按分组展示多组Angular Material MatTable
当前已实现单一数据源的MatTable,现需通过服务按weight字段分组,生成多个独立MatTable分别展示同组数据。以下是改造方案:
1. 创建数据分组服务
封装通用的分组逻辑,可在多个组件中复用:
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class DataGroupingService { // 按指定字段对数据进行分组 groupByField(data: any[], field: string): Map<string, any[]> { return data.reduce((groupMap, item) => { const groupKey = item[field]; if (!groupMap.has(groupKey)) { groupMap.set(groupKey, []); } groupMap.get(groupKey)!.push(item); return groupMap; }, new Map<string, any[]>()); } }
2. 改造组件逻辑
注入分组服务,处理原始数据为分组结构,并为每个分组创建独立数据源:
import { Component } from '@angular/core'; import { DataSource } from '@angular/cdk/collections'; import { Observable, of } from 'rxjs'; import { DataGroupingService } from './data-grouping.service'; const ELEMENT_DATA: any[] = [ {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'}, {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'}, {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'}, {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'}, {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'}, {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'}, {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'}, {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'}, {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'}, {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'}, {position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'}, {position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'}, {position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'}, {position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'}, {position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'}, {position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'}, {position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'}, {position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'}, {position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'}, {position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'}, ]; export class GroupedTableDataSource extends DataSource<any> { constructor(private dataSet: any[]) { super(); } connect(): Observable<any[]> { return of(this.dataSet); } disconnect() {} } @Component({ selector: 'app-grouped-tables', templateUrl: './grouped-tables.component.html' }) export class GroupedTablesComponent { columns = [ { columnDef: 'position', header: 'No.', cell: (element: any) => `${element.position}` }, { columnDef: 'name', header: 'Name', cell: (element: any) => `${element.name}` }, { columnDef: 'weight', header: 'Weight', cell: (element: any) => `${element.weight}` }, { columnDef: 'symbol', header: 'Symbol', cell: (element: any) => `${element.symbol}` }, ]; displayedColumns = this.columns.map(c => c.columnDef); groupedDataSources: Map<string, GroupedTableDataSource>; constructor(private groupingService: DataGroupingService) { // 按weight字段分组,为每个分组生成独立数据源 const groupedDataMap = this.groupingService.groupByField(ELEMENT_DATA, 'weight'); this.groupedDataSources = new Map( Array.from(groupedDataMap.entries()).map(([key, data]) => [key, new GroupedTableDataSource(data)]) ); } }
3. 修改HTML模板
循环渲染每个分组对应的MatTable,同时显示分组标识:
<div *ngFor="let [weightKey, tableDataSource] of groupedDataSources | keyvalue"> <h3>分组:Weight = {{ weightKey }}</h3> <mat-table [dataSource]="tableDataSource" class="mat-elevation-z2"> <ng-container *ngFor="let column of columns" [cdkColumnDef]="column.columnDef"> <mat-header-cell *cdkHeaderCellDef>{{ column.header }}</mat-header-cell> <mat-cell *cdkCellDef="let row">{{ column.cell(row) }}</mat-cell> </ng-container> <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row> <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row> </mat-table> <br> </div>
内容的提问来源于stack exchange,提问作者Eren Jaeger
相关产品推荐
相关产品推荐

