将地图对象(带有动态键和值)渲染到有角度的mat-table(列和行作为地图的键和值)中

西哈德·萨拉姆(Shihad Salam)

我正在使用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

yurzui

以下模板将为您工作:

<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>

Stackblitz示例

请注意,为了获得更好的性能,您可以({}).constructor.keys(map)使用自己的自定义管道来替换,例如map | keys管道在哪里:

@Pipe({ name: 'keys' })
export class EnumToArrayPipe implements PipeTransform {
  transform(data: {}) {
    return Object.keys(data);
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

比较作为地图的键和值的集合的大小

在Angular JS中动态构造具有键和值的对象作为列表

如何获取地图的所有键和值并以args形式传递给golang中的函数?

jQuery的地图数组键和值

Angular 2从带有动态键的json数组中获取键和值

在Groovy中创建具有两个集合的地图-带有键和值

mongodb的Apache Drill。在地图中查询具有特定键和值的文档

坚持以枚举为键的地图和对象列表为值的地图

ReactJS:使用带有地图键的对象渲染对象

如何从地图对象中删除带有对象键的对象?

映射到列的键和值到行的键

映射到列的键和值到行的键

在VUE模板中动态传递所有对象的键和值作为HTML属性

从地图中删除键和值

如何将Firebase文档中的所有字段检索到地图或“键:值”对

使用Scala和Spark读取文本文件中的键/值对,键作为列名,值作为行

如何将多个具有相同的键和列表作为值的地图组合在一起

我在Pandas数据框列中有字典作为值。我想使键列和值作为列值

是否有一种惯用的方法在Clojure中的地图中找到匹配的键和值?

界面作为地图键和值的问题

将键和值转换为具有单个键/值的对象的类型安全方法

获取地图的键和值

Shell Scrip:将行拆分为数组/YAML 格式,并带有相应的键和值

从字典中添加具有键和值的列

将字典列扩展为具有字典键和字典值的行作为熊猫中的新列值

Javascript将数组转换为具有备用索引作为键和值的对象

从具有 2 列(键和值)的表中,如何返回带有列值的 json?

我有一个包含拖曳地图的列表,我会用飞镖打印地图一的键和值

使用 Jolt 使用键中的值创建带有硬编码键和值的 JSON 数组