如何使用 Angular4 *ngFor 创建数据 table?
How can I use Angular4 *ngFor to create a data table?
我正在开发一个项目,使用 Angular4 从 API 获取数据并使用 *ngFor 呈现数据 table。因为我还有更多具有相同结构的 aips,所以我想使用 (key, value) 对来显示它们。在 AngularJS 中,我正确地渲染了 table,如下所示:
<!--This is Good in AngularJS-->
<table>
<thead>
<tr>
<th ng-repeat="(key, value) in data.items[0]"> {{key}}
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat ="data in data.items >
<td ng-repeat="(key,value) in data">{{ value }}</td>
</tr>
</tbody>
</table>
但是,table 在 Angular4 中没有正确显示。来自 API 的原始 json 数据显示如下:
{
items: [
{
status: "Sold - Payment Received",
count: 30,
loans: 8,
dl_loans: 8,
avg_available: 149.5,
min: 28,
max: 346,
principal: 13452.37,
closed: 0,
chrg_of_balance: 0,
final_balance: 0
},
{
status: "At Auction - Awaiting Info",
count: 4,
loans: 4,
dl_loans: 4,
avg_available: 70.45,
min: 36,
max: 102,
principal: 11727.8,
closed: 0,
chrg_of_balance: 0,
final_balance: 0
},
...
}
这是我的 app.component.ts:
ngOnInit(): void {
this.dataService.getData().subscribe(
(data) => {
this.data = data.items;
this.titles = data.items[0];
}
);
}
我为过滤键和值创建了一个 pip.ts:
import { PipeTransform, Pipe } from '@angular/core';
@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
transform(value, args:string[]) : any {
let keys = [];
for (let key in value) {
keys.push({key: key, value: value[key]});
}
return keys;
}
}
在 Angular4 中 HTML:
<!--This is Bad in Angular4-->
<table>
<thead align="center">
<tr>
<th *ngFor = "let item of titles | keys">
{{item.key}}
</th>
</tr>
</thead>
<tbody>
<tr>
<td *ngFor = "let item of data | keys ">
{{item.value | json }}
</td>
</tr>
</tbody>
</table>
然而 UI 中的 thead 显示正常,但 tbody 部分显示包括整个对象(部分):
status
---------------------------------------
{
status: "Sold - Payment Received",
count: 30,
loans: 8,
dl_loans: 8,
avg_available: 149.5,
min: 28,
max: 346,
principal: 13452.37,
closed: 0,
chrg_of_balance: 0,
final_balance: 0
}
--------------------------------------
任何人都知道我怎样才能正确渲染这个 table?先谢谢你了!
使用不同的管道而不是您正在使用的管道,这将 return 您的对象的键
import { PipeTransform, Pipe } from '@angular/core';
@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
transform(value, args:string[]) : any {
let keys = [];
for (let key in value) {
keys.push(key);
}
return keys;
}
}
您应该将模板代码更改为
<thead align="center">
<tr>
<th *ngFor = "let item of titles | keys">
{{item}}
</th>
</tr>
</thead>
<tbody>
<tr *ngFor = "let item of data ">
<td *ngFor = "let key of item | keys " >
{{item[key]}}
</td>
</tr>
</tbody>
这应该可以解决问题
我正在开发一个项目,使用 Angular4 从 API 获取数据并使用 *ngFor 呈现数据 table。因为我还有更多具有相同结构的 aips,所以我想使用 (key, value) 对来显示它们。在 AngularJS 中,我正确地渲染了 table,如下所示:
<!--This is Good in AngularJS-->
<table>
<thead>
<tr>
<th ng-repeat="(key, value) in data.items[0]"> {{key}}
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat ="data in data.items >
<td ng-repeat="(key,value) in data">{{ value }}</td>
</tr>
</tbody>
</table>
但是,table 在 Angular4 中没有正确显示。来自 API 的原始 json 数据显示如下:
{
items: [
{
status: "Sold - Payment Received",
count: 30,
loans: 8,
dl_loans: 8,
avg_available: 149.5,
min: 28,
max: 346,
principal: 13452.37,
closed: 0,
chrg_of_balance: 0,
final_balance: 0
},
{
status: "At Auction - Awaiting Info",
count: 4,
loans: 4,
dl_loans: 4,
avg_available: 70.45,
min: 36,
max: 102,
principal: 11727.8,
closed: 0,
chrg_of_balance: 0,
final_balance: 0
},
...
}
这是我的 app.component.ts:
ngOnInit(): void {
this.dataService.getData().subscribe(
(data) => {
this.data = data.items;
this.titles = data.items[0];
}
);
}
我为过滤键和值创建了一个 pip.ts:
import { PipeTransform, Pipe } from '@angular/core';
@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
transform(value, args:string[]) : any {
let keys = [];
for (let key in value) {
keys.push({key: key, value: value[key]});
}
return keys;
}
}
在 Angular4 中 HTML:
<!--This is Bad in Angular4-->
<table>
<thead align="center">
<tr>
<th *ngFor = "let item of titles | keys">
{{item.key}}
</th>
</tr>
</thead>
<tbody>
<tr>
<td *ngFor = "let item of data | keys ">
{{item.value | json }}
</td>
</tr>
</tbody>
</table>
然而 UI 中的 thead 显示正常,但 tbody 部分显示包括整个对象(部分):
status
---------------------------------------
{
status: "Sold - Payment Received",
count: 30,
loans: 8,
dl_loans: 8,
avg_available: 149.5,
min: 28,
max: 346,
principal: 13452.37,
closed: 0,
chrg_of_balance: 0,
final_balance: 0
}
--------------------------------------
任何人都知道我怎样才能正确渲染这个 table?先谢谢你了!
使用不同的管道而不是您正在使用的管道,这将 return 您的对象的键
import { PipeTransform, Pipe } from '@angular/core';
@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
transform(value, args:string[]) : any {
let keys = [];
for (let key in value) {
keys.push(key);
}
return keys;
}
}
您应该将模板代码更改为
<thead align="center">
<tr>
<th *ngFor = "let item of titles | keys">
{{item}}
</th>
</tr>
</thead>
<tbody>
<tr *ngFor = "let item of data ">
<td *ngFor = "let key of item | keys " >
{{item[key]}}
</td>
</tr>
</tbody>
这应该可以解决问题