Angular 角垫表在每行中显示动态键值对
我有一个JSON格式的键值对数据,如下所示。钥匙在本质上是动态的。没有特定的键集将成为JSON的一部分。我想使用AngularAngular 角垫表在每行中显示动态键值对,angular,typescript,angular-material,Angular,Typescript,Angular Material,我有一个JSON格式的键值对数据,如下所示。钥匙在本质上是动态的。没有特定的键集将成为JSON的一部分。我想使用Angularmat table以表格格式显示它们 var data = { "cars" : 24, "fruit" : "apple", "phone" : "Iphone", "food" : "Burger" }; 我的表输出应该是: 表标题应包含2列键和值 每行数据都应该高于动态json键值 预期表格输出: 将对象转换为数组 dataSource = [];
mat table
以表格格式显示它们
var data = {
"cars" : 24,
"fruit" : "apple",
"phone" : "Iphone",
"food" : "Burger"
};
我的表输出应该是:
- 表标题应包含2列键和值
- 每行数据都应该高于动态json键值李>
将对象转换为数组
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>
钥匙
{{element.key}
价值
{{element.value}}
将对象转换为数组
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>
钥匙
{{element.key}
价值
{{element.value}}
无需将对象转换为数组。
您可以轻松使用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>
钥匙
{{element.key}
价值
{{element.value}}
您可以在中检查它的工作方式。无需将对象转换为数组。 您可以轻松使用
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>
钥匙
{{element.key}
价值
{{element.value}}
您可以在中检查它的工作方式。如果您有一个打字错误,那么它就是{key:key:value:this.data[key]}。您还可以使用map,而不是for
dataSource=Object.keys(this.data).map(key=>({key:key,value:this.data[key]}))
@Eliseo这不是打字错误,这是带有ES6的JavaScript中的对象属性值速记。。在
中,无需调用其他方法,如Object.keys
和map
function@AhmedKesha谢谢你的快速回复。我尝试了该解决方案,但遇到错误:找不到id为“key”的列。这是链接,你能看一下吗?@vamsi我更新了我的答案,你只需修改matColumnDef=“key”
,matColumnDef=“value”你有一个打字错误,是{key:key:value:this.data[key]}
。您还可以使用map,而不是fordataSource=Object.keys(this.data).map(key=>({key:key,value:this.data[key]}))
@Eliseo这不是打字错误,这是带有ES6的JavaScript中的对象属性值速记。。在
中,无需调用其他方法,如Object.keys
和map
function@AhmedKesha谢谢你的快速回复。我尝试了该解决方案,但遇到错误:找不到id为“key”的列。这是链接,你能看一下吗?@vamsi我更新了我的答案,你只需修改matColumnDef=“key”
,而matColumnDef=“value”我必须按键和值对值进行排序。在这种情况下我如何使用matSort?如果我以后有空,我会尝试为这种情况找到解决方案,但对于排序或筛选,您可能应该将对象转换为数组。你的生活会更轻松。我必须按键和值对值进行排序。在这种情况下我如何使用matSort?如果我以后有空,我会尝试为这种情况找到解决方案,但对于排序或筛选,您可能应该将对象转换为数组。你的生活会更轻松。