angular - 在动态材料表模板中显示嵌套对象
问题描述
我想在表格中显示 weight.name。我能够通过转换数据来做到这一点。但是,我不想以任何形式操作数据。我尝试在行模板 ( ) 中更改weight
为,但根本没有返回任何数据。weight.name
displayedColumns
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>
应用组件.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
解决方案
您的问题是您将权重列显示为 row[col] 或 rowInstance['weight'] 显示toString()
权重对象的[object object]
.
将您的表格模板修改为:
<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
将您的数据模型上游转换/展平为如下所示,并使您的模板保持不变。
[{position: 1, name: 'Hydrogen', weightName: 5.55, symbol: 'H'},...]
推荐阅读
- javascript - 仅使用 Javascript 验证域名字符串 .com 和 .net 允许
- java - Java:如何从方法内部更改整数数组参数
- rest - 在 HTTP 上运行的 Soap 服务是 REST 服务吗
- pointers - 复制一片指针(指向新值)
- python - 按键删除和编辑值
- c++ - 在 UWP 页面之间传递参数的正确方法?
- java - 在 Java 中使用 JNA 关闭监视器无法按预期工作
- php - 如何使用 foreach 循环将 $_POST 保存到文本文件
- python - 无法写入文件以保存游戏分数
- mongodb - 使用 vue js 和 axios 按 id 显示单个记录