【问题标题】:Angular mat-table display dynamic key value pair in each rowAngular mat-table 在每一行中显示动态键值对
【发布时间】:2020-03-14 10:36:59
【问题描述】:

我有一个 JSON 格式的键值对数据,如下所示。键本质上是动态的。没有特定的密钥集将成为 JSON 的一部分。我想使用 Angular mat-table 以表格格式显示它们。

var data = {
 "cars" : 24,
 "fruit" : "apple",
 "phone" : "Iphone",
 "food" : "Burger"
};

我的表格输出应该是:

  • 表头应包含 2 列 KEY 和 VALUE
  • 每一行数据都应该高于动态json键值。

预期的表格输出:

【问题讨论】:

    标签: angular typescript angular-material


    【解决方案1】:

    将你的对象转换成数组

      dataSource = [];
      var data = {
        cars: 24,
        fruit: "apple",
        phone: "Iphone",
        food: "Burger"
      };
    
      for (const key in data) {
        dataSource.push({ key, value: data[key] });
      }
    

    并在角材料中使用它

    .ts 文件

    import { Component } from "@angular/core";
    export interface RowElement {
      key: string;
      value: string;
    }
    @Component({
      selector: "table-basic-example",
      styleUrls: ["table-basic-example.css"],
      templateUrl: "table-basic-example.html"
    })
    export class TableBasicExample {
      data = {
        cars: 24,
        fruit: "apple",
        phone: "Iphone",
        food: "Burger"
      };
    
      displayedColumns: string[] = ["key", "value"];
      dataSource: RowElement[];
    
      constructor() {
        for (const key in this.data) {
          this.dataSource.push({ key, value: this.data[key] });
        }
      }
    }
    

    .html 文件

    <table mat-table [dataSource]="dataSource">
      <!-- Key Column -->
      <ng-container matColumnDef="key">
        <th mat-header-cell *matHeaderCellDef>Key</th>
        <td mat-cell *matCellDef="let element">{{element.key}}</td>
      </ng-container>
    
      <!-- Value Column -->
      <ng-container matColumnDef="value">
        <th mat-header-cell *matHeaderCellDef>Value</th>
        <td mat-cell *matCellDef="let element">{{element.value}}</td>
      </ng-container>
    
      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
    

    【讨论】:

    • 你有一个拼写错误,是{key:key:value:this.data[key]}。你也可以使用地图,而不是dataSource = Object.keys(this.data).map(key=&gt;({key:key,value:this.data[key]}))
    • @Eliseo 这不是拼写错误,即Object Property Value Shorthand in JavaScript with ES6
    • @Eliseo for .. in 无需调用其他方法,如 Object.keysmap 函数
    • @AhmedKesha 感谢您的快速回复。我尝试了该解决方案,但收到 ERROR 错误:找不到 ID 为“key”的列。这是链接stackblitz.com/edit/angular-mat-table-ex你可以看一次吗?
    • @vamsi 我更新了我的答案你只是为了修改matColumnDef="key" 和 ,matColumnDef="value"
    【解决方案2】:

    不需要将对象转换成数组。 您可以轻松使用keyvalue 管道。

    在ts文件中:

    // properties of the class
    displayedColumns: string[] = ['key', 'value'];
    dataSource = data;
    
    // use this method if you want to keep the order of the object properties
    public orderByKey(a, b) {
        return a.key;
      }
    

    在html文件中:

    <table mat-table [dataSource]="dataSource | keyvalue:orderByKey" class="mat-elevation-z8">
      <ng-container matColumnDef="key">
        <th mat-header-cell *matHeaderCellDef> Key </th>
        <td mat-cell *matCellDef="let element"> {{element.key}} </td>
      </ng-container>
    
      <ng-container matColumnDef="value">
        <th mat-header-cell *matHeaderCellDef> Value </th>
        <td mat-cell *matCellDef="let element"> {{element.value}} </td>
      </ng-container>
    
      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
    

    你可以在this stackblitz查看它是如何工作的

    【讨论】:

    • 我必须按键和值对值进行排序。在这种情况下如何使用 matSort?
    • 如果我以后有空闲时间,我会尝试为这种情况找到解决方案,但对于排序或过滤,您可能应该将对象转换为数组。你的生活会更轻松。
    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2020-06-26
    • 2018-03-25
    相关资源
    最近更新 更多