【问题标题】:Display a nested object in a dynamic material table template在动态材料表模板中显示嵌套对象
【发布时间】:2020-12-04 11:14:09
【问题描述】:

我想在表格中显示 weight.name。我能够通过转换数据来做到这一点。但是,我不想以任何形式操作数据。我尝试将行模板(displayedColumns)中的weight更改为weight.name,但根本没有返回任何数据。

App.component.html

<table mat-table [dataSource]="dataSource">
  <ng-container *ngFor="let col of displayedColumns" [matColumnDef]="col">
    <th mat-header-cell *matHeaderCellDef> {{col}} </th>
    <td mat-cell *matCellDef="let element"> {{element[col]}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

App.component.ts

import { Component } from '@angular/core';

export interface PeriodicElement {
  name: string;
  position: number;
  weight: any;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: {name: 5.55}, symbol: 'H'},
  {position: 2, name: 'Helium', weight: {name: 5.55}, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: {name: 5.55}, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: {name: 5.55}, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: {name: 5.55}, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: {name: 5.55}, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: {name: 5.55}, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: {name: 5.55}, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: {name: 5.55}, symbol: 'F'},
  {position: 10, name: 'Neon', weight: {name: 5.55}, symbol: 'Ne'},
];

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

这是供参考的 stackblitz 链接。 https://stackblitz.com/edit/angular-mat-table-dynamic-columns-x36dvr?file=src%2Fapp%2Fapp.component.ts

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    您的问题是您将权重列显示为 row[col] 或 rowInstance['weight'] 显示权重对象的 toString() 导致 [object object]

    1. 将您的表格模板修改为:

      <table mat-table [dataSource]="dataSource">
           <ng-container matColumnDef="position"> 
               <mat-header-cell *matHeaderCellDef> Position </mat-header-cell>
               <mat-cell *matCellDef="let row">{{row.position}}</mat-cell>
           </ng-container> 
           <ng-container matColumnDef="name"> 
               <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
               <mat-cell *matCellDef="let row">{{row.name}}</mat-cell>
           </ng-container>
           <ng-container matColumnDef="weightName"> 
               <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
               <mat-cell *matCellDef="let row">{{row.weight.name}}</mat-cell>
           </ng-container>
           <ng-container matColumnDef="symbol"> 
               <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
               <mat-cell *matCellDef="let row">{{row.symbol}}</mat-cell>
           </ng-container>
      
           <mat-header-row *matHeaderRowDef="displayedColumns">
           </mat-header-row>
           <mat-row *matRowDef="let row; columns: displayedColumns;">
           </mat-row>
       </table>
      

    在您的 ts 文件中,将显示列修改为:

      `displayedColumns: string[] = ['position', 'name', 'weightName', 'symbol'];`
    

    https://stackblitz.com/edit/angular-mat-table-dynamic-columns-w7do4c?file=src/app/app.component.html

    1. 将您的上游数据模型转换/展平为如下所示,并保持您的模板不变。

      [{position: 1, name: 'Hydrogen', weightName: 5.55, symbol: 'H'},...]

    【讨论】:

    • 有没有办法在不转换数据或更改模板的情况下做到这一点?为什么我不能将 weight.name 绑定到显示的列,为什么我没有取回任何数据
    • 为什么不使用点表示法而不是转换数据?
    • @Nathan 你绝对可以,但是,给定来自问题的模板,他们正在遍历对象 let col of displayedColumns 中的每个字段,并且基本上从每个属性中检索 toString() 值。由于 weight 是一个没有toString() 函数的嵌套对象,所以会显示[object object]。我建议替代方案,这两种方法都有效(例如),所以我不确定为什么会否决。
    【解决方案2】:

    我遇到过类似的问题,我解决了如下。

    1. 更新displayedColumns: string[] = ['position', 'name', 'weight.name', 'symbol'];
    2. app.component.ts中写一个方法如下
    findColumnValue = (element:unknown, column:string):string => <string>column.split('.').reduce((acc, cur) => acc[cur], element);
    
    1. 更新html文件如下
    <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> {{column.split('.')[0]}} </th>
        <td mat-cell *matCellDef="let element"> {{findColumnValue(element, column)}} </td>
    </ng-container>
    
    1. 如果你对eval方法没问题,你可以如下使用它
    findColumnValue = (element:unknown, column:string):string => eval(`element.${column}`);
    

    【讨论】:

      【解决方案3】:

      您可以在显示的列数组中使用点表示法访问嵌套对象

      例子:

        displayedColumns: string[] = ['position', 'name', 'weight.name', 'symbol'];
      

      在表格 HTML 中,您需要在 matColDef 中引用点符号 weight.name

      您的 HTML:

        <ng-container matColumnDef="weight.name"> 
           <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
           <mat-cell *matCellDef="let row">{{row.weight.name}}</mat-cell>
        </ng-container>
      

      【讨论】:

      • 我想用模板,不想改。
      • 您仍在使用模板,但需要引用正确的列matColumnDef="weight.name"
      • 怎么办?我使用 *ngFor 而不是使用列名本身
      猜你喜欢
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2019-01-29
      相关资源
      最近更新 更多