【问题标题】:Angular Material Table how to pass object to displayedColumns instead of arrayAngular Material Table如何将对象传递给displayedColumns而不是数组
【发布时间】:2019-11-02 19:00:54
【问题描述】:

我正在使用材料表在 Angular8 中构建一个表。

我正在使用一个字符串数组来定义 displayColumns 并将每个数组值传递给 matColumnDef 指令。

这工作正常。

TS

displayedColumns: any[] = ['username', 'firstname', 'lastname'];

HTML

<div class="table-container mat-elevation-z8">
  <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
    <ng-container *ngFor="let column of displayedColumns;" [matColumnDef]="column">
      <th mat-header-cell *matHeaderCellDef> {{ column }} </th>
      <td mat-cell *matCellDef="let element"> {{ element[column] }} </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>
</div>

问题:是否可以将显示的列定义为对象数组而不是字符串数组?

我想为列显示值设置一个不同的值,也可能有条件地设置一些其他列属性。

示例: 请注意,displayedColumns 是一个对象数组。这不起作用。

TS

  displayedColumns: any[] = [
    { name: 'username', display: 'User Name' },
    { name: 'firstname', display: 'First Name' },
    { name: 'lastname', display: 'Last Name' }
  ];

HTML

<div class="table-container mat-elevation-z8">
  <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
    <ng-container *ngFor="let column of displayedColumns;" [matColumnDef]="column.name">
      <th mat-header-cell *matHeaderCellDef> {{ column.display }} </th>
      <td mat-cell *matCellDef="let element"> {{ element[column.name] }} </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>
</div>

错误信息:

Could not find column with id "[object Object]".
    at getTableUnknownColumnError (table.js:890)
    at table.js:1973

我认为错误表明 mat-table 正在迭代显示的列,期望一个数组元素,但得到一个对象。 有没有办法解决这个问题或以其他方式实现这一要求?

【问题讨论】:

  • 这就是你在看的for

标签: angular angular-material-table


【解决方案1】:

我能够通过创建一个单独的对象来存储表列名称和显示名称来实现这一点。

  // Table columns
  initColumns: any[] = [
    {
      name: 'username',
      display: 'User Name'
    },
    {
      name: 'firstname',
      display: 'First Name'
    },
    {
      name: 'Description',
      display: 'Description'
    }
  ];

displayedColumns 设置为每个列的“名称”属性的字符串数组。

  // Displayed columns array of strings
  displayedColumns: any[] = this.initColumns.map(col => col.name);

ngFor 迭代 initColumns 对象数组并设置 matColumnDef,显示显示名称和行值。

<ng-container [matColumnDef]="column.name" *ngFor="let column of initColumns;">
<th mat-header-cell *matHeaderCellDef> {{column.display}}</th>
<td mat-cell *matCellDef="let element"> {{element[column.name]}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>

我从a similar question 的回答中得到了这个想法。请注意,我的特定解决方案不需要 trackbyindex 部分。

【讨论】:

  • 这两个做这个把戏,所以我们需要两列的变量——比如matColumns(又名displayedColumns)和columns(比如对象数组),以便在表格行内使用你想要的任何属性。
  • 太棒了!这对我尝试配置的 mat 表数据源有很大帮助。基本上我有一个学生的名字作为最左边的列标题,以及动态数量的日期作为其余列。在这些日期列的单元格中,我需要一个等级,它位于我传入的对象本身上,但属性名称不同。在这种情况下,遵循 initColumns/displayColumns 效果很好。谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-01-31
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多