【问题标题】:How can I use Angular4 *ngFor to create a data table?如何使用 Angular4 *ngFor 创建数据表?
【发布时间】:2018-02-09 12:43:01
【问题描述】:

我正在使用 Angular4 从 API 获取数据并使用 *ngFor 呈现数据表的项目。因为我还有更多结构相同的 aip,所以我想使用 (key, value) 对来显示它们。在 AngularJS 中,我正确地呈现了这样的表格:

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

但是,表格在 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
 }
--------------------------------------

任何人都知道如何正确呈现此表?提前谢谢你!

【问题讨论】:

  • 您不能循环 title,因为标题被放置到数据中的单个项目 this.titles = data.items[0]; 可能正在更改为数据将起作用
  • stackoverflow.com/a/45880284/7491209 这可能会有所帮助

标签: angular angularjs-ng-repeat ngfor


【解决方案1】:

使用不同的管道而不是您正在使用的管道,它只会返回对象的键

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>

这应该可以解决问题

【讨论】:

  • 谢谢。但我有其他 api 具有相同的结构但不同的内容,这意味着它们看起来都像 {items:[{.....},{.....},...]} 是我可以使用的任何方式AngularJS 中的 (key, value) 方式来显示它们而不是特定的键名?
  • 更新了我的答案。请检查
  • 嗨@Vikhyath,非常感谢!它改进了,渲染了正确的行,但表格单元格是空的,并给我一个错误:“找不到一个不同的支持对象 '[object Object]' 类型为 'object'。NgFor 只支持绑定到 Iterables,如数组。”有什么提示吗?
  • 对不起,我忘了那个管道。请检查更新的答案
  • 嗨@Vikhyath,首先非常感谢你的好意!这个版本不如上一个版本好,在这个版本中,它仍然在 tbody 中显示整个对象,就像我发布的那样,它给了我错误的行号。我认为 中一定有一些内容,就像您之前发布的那样:“ ”。这样,它至少给了我正确的表格(列和行)。还有更多线索吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
  • 2018-05-12
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多