简体   繁体   English

Angular:将JSON数组转换为HTML表时动态查找标题

[英]Angular: Dynamically find headers when converting JSON array to HTML table

In Angular, I want to convert a JSON array to an HTML table.在 Angular 中,我想将 JSON 数组转换为 HTML 表。

I have seen an old answer for AngularJS:我已经看到了 AngularJS 的旧答案

<table>
    <thead>
      <tr>
        <th ng-repeat="(key, value) in records[0]">{{key}}</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="(key, value) in records">
        <td ng-repeat="(key, value) in value">
          {{value}}
        </td>
      </tr>
    </tbody>
</table>

JSON looks like this: JSON 看起来像这样:

[{
    "Name": "Alfreds Futterkiste",
    "City": "Berlin",
    "Country": "Germany"
}, {
    "Name": "Berglunds snabbköp",
    "City": "Luleå",
    "Country": "Sweden"
}, {
    "Name": "Centro comercial Moctezuma",
    "City": "México D.F.",
    "Country": "Mexico"
}]

I've tried to translate it to the Angular syntax.我试图将它翻译成 Angular 语法。 Here is what I got so far:这是我到目前为止得到的:

<table>
    <thead>
      <tr>
        <th *ngFor="let item of records[0]  | keyvalue">{{item.key}}</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let item of records">
        <td *ngFor="let item1 of item | keyvalue">
          {{item1.value}}
        </td>
      </tr>
    </tbody>
</table>

Right now it's failing to compile because records[0] is undefined... how can I translate this expression to the newer syntax (or create something equivalent)?现在它无法编译,因为records[0]未定义......我如何将此表达式转换为较新的语法(或创建等效的东西)?

UPDATE 1:更新 1:

I have a partial solution.我有一个部分解决方案。 However with this partial solution the rendered table is not completely identical to the older AngularJS rendition (because it creates multiple unnecessary header rows, which only one of them is populated, as opposed to only one header row in the older rendition).然而,使用这种部分解决方案,呈现的表格与旧的AngularJS版本并不完全相同(因为它创建了多个不必要的标题行,其中只有一个被填充,而不是旧版本中只有一个标题行)。

<table style="border-collapse: collapse;">
    <thead *ngFor="let item of records; let last=last">
      <tr *ngIf="last">
        <th *ngFor="let item1 of item | keyvalue">
          {{item1.key}}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let item of records">
        <td *ngFor="let item1 of item | keyvalue">
          {{item1.value}}
        </td>
      </tr>
    </tbody>
</table>

Do you have a better way to do it, possibly similar to the older AngularJS version?你有更好的方法吗,可能类似于旧的AngularJS版本?

UPDATE 2:更新 2:

In Angular, I access the JSON data through a request from Angular that is then redirected to a back end service.在 Angular 中,我通过来自 Angular 的请求访问 JSON 数据,然后将其重定向到后端服务。 That service may read a file, or get the data from a database.该服务可以读取文件,或从数据库中获取数据。 When the back end service has the data ready, it returns the data to the Angular request.当后端服务准备好数据时,它会将数据返回给 Angular 请求。 The code on the Angular end looks like this: Angular 端的代码如下所示:

  HTML:

  <div>
    <h3>Test Post Request</h3>
    <button (click)="postData()">Click Me</button>
    <div>Response: {{records}}</div>
  </div>

  TypeScript:

  private dataPostTestUrl = '/api/postTest';
  records: string | undefined;

  public postData(): void {
    this.appService.sendData().subscribe((data: any) => {
      this.records = data.content;
    });
  }

  public sendData(): Observable<any> {
    return this.http.post(this.dataPostTestUrl, {});
  }

I think maybe you need to define records in the component.我想也许你需要在组件中定义records

records = [{
    "Name": "Alfreds Futterkiste",
    "City": "Berlin",
    "Country": "Germany"
}, {
    "Name": "Berglunds snabbköp",
    "City": "Luleå",
    "Country": "Sweden"
}, {
    "Name": "Centro comercial Moctezuma",
    "City": "México D.F.",
    "Country": "Mexico"
}];

I am fairly certain this is what you need: https://stackblitz.com/edit/angular-ivy-n7irpw?file=src/app/hello.component.ts我相当确定这是您需要的: https ://stackblitz.com/edit/angular-ivy-n7irpw?file=src/app/hello.component.ts

Take a look at how I import the file in the app.component.ts As well as how I loop in the HTML.看看我如何在app.component.ts中导入文件,以及我如何在 HTML 中循环。

Let me know if it helps!让我知道它是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM