我正在使用angular8作为前端和SpringBoot作为后端来开发POC。我的后端函数向我返回了List < Map < String, Object > >
包含动态键和值的Map对象列表()。我需要在mat-dialog-content内部使用mat-table在前端角度使用此映射数组。我坚持将键和值定义为mat表的必需属性。请帮助我填充地图对象的动态列表(列标题作为地图键,行作为地图值)。
在下面发布了我的组件和html文件:
零件:
export class ProfileDialogComponent {
username: String = "";
mapArray: Map < String, Object > [] = [];
constructor(private dialogRef: MatDialogRef < ProfileDialogComponent > ,
@Inject(MAT_DIALOG_DATA) data, private userService: UserService) {
this.username = data.username;
this.userService.getImportedUserCareer(this.username).subscribe(data => {
this.mapArray = data;
});
}
}
的HTML:
<div>
<mat-dialog-content *ngFor="let map of mapArray">
<mat-table class="mat-elevation-z8" [dataSource]="map">
<ng-container matColumnDef="column" *ngFor="let column of map | keyvalue">
<mat-header-cell *matHeaderCellDef>{{column}}</mat-header-cell>
<mat-cell *matCellDef="let element">{{element[column]}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="map"></mat-header-row>
<mat-row *matRowDef="let row; columns: map"></mat-row>
</mat-table>
</mat-dialog-content>
</div>
此时,我收到以下错误:错误:提供了重复的列定义名称:“ column”。
我在组件中填充了映射数组,其中包含字符串和字符串值的映射。但是html中的实现逻辑并不完整,如果有人可以指导我如何在mat table中填充地图数据,那将非常有帮助。
PS:我已经在mat-dialog-content中迭代了map数组,以便将每个map对象都保存在mat-table中,因此应该将map键和值填充为每个mat-dialog中的每个mat-table列标题和行-内容。
以下是数据示例
[
{
"Test Matches": 320,
"Runs": 17500,
"High Score": 242,
"Batting Avg": 65.42,
"Wickets": 14,
"Bowling Avg": 31.76,
"Best Bowling": "1/34",
"Catches": 173,
"MoS": 25
},
{
"ODI Matches": 150,
"Runs": 15750,
"High Score": 184,
"Batting Avg": 62.75,
"Catches": 173,
"MoM": 54
}
]
请帮忙!
谢谢,Shihad
以下模板将为您工作:
<div *ngFor="let map of mapArray">
<mat-table class="mat-elevation-z8" [dataSource]="[map]">
<ng-container [matColumnDef]="column.key" *ngFor="let column of map | keyvalue">
<mat-header-cell *matHeaderCellDef>{{ column.key }}</mat-header-cell>
<mat-cell *matCellDef="let element">{{ column.value }}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="({}).constructor.keys(map)"></mat-header-row>
<mat-row *matRowDef="let row; columns: ({}).constructor.keys(map)"></mat-row>
</mat-table>
</div>
请注意,为了获得更好的性能,您可以({}).constructor.keys(map)
使用自己的自定义管道来替换,例如map | keys
管道在哪里:
@Pipe({ name: 'keys' })
export class EnumToArrayPipe implements PipeTransform {
transform(data: {}) {
return Object.keys(data);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句