简体   繁体   English

如何在 Angular 中的 object 中对对象数组进行数据绑定?

[英]How to databind an array of objects within an object in Angular?

Trying to get data to show up in my table.试图让数据显示在我的表格中。 I don't think I'm doing the data binding correctly.我认为我没有正确地进行数据绑定。

One of the interfaces returns an array of a different interface like so:其中一个接口返回一个不同接口的数组,如下所示:

import { ExceptionDetailLine } from './exception-detail-line-interface';

export interface ExceptionReportData {
  exceptionReportTitle: string,
  exceptionReportLines: Array<ExceptionDetailLine>
}

ExceptionDetailLine interface: ExceptionDetailLine接口:

export interface ExceptionDetailLine {
  itemId: string,
  altItemId: string,
  genName: string,
  stationName: string,
  hospitalCode: string,
  facility: string,
  reason: string
}

How do I get exceptionReportLines to display in the table?如何让exceptionReportLines显示在表格中?

Here is how I am trying with no luck:这是我没有运气的尝试:

<p-table class="table table-striped"
             [columns]="cols"
             [value]="sessionReportData"
             [paginator]="true"
             sortMode="multiple"
             sortField="station"
             [rows]="10"
             [totalRecords]="totalCases"
             [rowsPerPageOptions]="[10, 25, 50]"
             [showCurrentPageReport]="true"
             [responsive]="true"
             [resizableColumns]="true">
      <ng-template pTemplate="header" let-columns>
        <ng-template pTemplate="colgroup" let-columns>
          <colgroup>
            <col *ngFor="let col of columns">
          </colgroup>
        </ng-template>
        <tr>
          <th *ngFor="let col of columns" pResizableColumn [pSortableColumn]="col.field">
            {{ col.header }}
            <p-sortIcon [field]="col.header"></p-sortIcon>
          </th>
        </tr>
      </ng-template>
      <ng-template pTemplate="body">
        <tr *ngFor="let s of sessionReportData.exceptionReportLines">
          <td>{{ s.itemId }}</td>
          <td>{{ s.altItemId }}</td>
          <td>{{ s.genName }}</td>
          <td>{{ s.stationName }}</td>
          <td>{{ s.hospitalCode }}</td>
          <td>{{ s.facility }}</td>
          <td>{{ s.reason }}</td>
        </tr>
      </ng-template>

Here is how the data is retrieved in my component.ts:以下是在我的 component.ts 中检索数据的方式:

public async orderExceptionReportData(): Promise<void> {
    return this.orderExceptionReportService.OrderExceptionReport(this.sessionReportFilter)
      .then(
        data => {
          this.sessionReportData = data;
          console.log(this.sessionReportData)

        });
  }

this.sessionReportData is assigned values asynchronously. this.sessionReportData被异步赋值。 Try to wrap the table in a *ngIf directive to check if it's available before accessing it.尝试将表包装在*ngIf指令中,以在访问它之前检查它是否可用。

<ng-container *ngIf="sessionReportData">
  <p-table>
    ...
  </p-table>
</ng-container>

You could also use async pipe and avoid storing the values in a variable.您还可以使用async pipe 并避免将值存储在变量中。

First of all, you should use Observable instead of promise to get your sessionReportData.首先,你应该使用 Observable 而不是 promise 来获取你的 sessionReportData。

For your table, you give to PrimeNG an object instead of an array, you must give to the table an array object.对于你的表,你给 PrimeNG 一个 object 而不是一个数组,你必须给这个表一个数组 object。 Moreover, you should use the variable generate by PrimeNG as you did for your columns此外,您应该使用 PrimeNG 生成的变量,就像您对列所做的那样

<p-table class="table table-striped"
             [columns]="cols"
             [value]="sessionReportData?.exceptionReportLines">
...
  <ng-template pTemplate="body" let-exceptionReportLine>
        <tr>
          <td>{{ exceptionReportLine.itemId }}</td>
          <td>{{ exceptionReportLine.altItemId }}</td>
          <td>{{ exceptionReportLine.genName }}</td>
          <td>{{ exceptionReportLine.stationName }}</td>
          <td>{{ exceptionReportLine.hospitalCode }}</td>
          <td>{{ exceptionReportLine.facility }}</td>
          <td>{{ exceptionReportLine.reason }}</td>
        </tr>
  </ng-template>
<p-table>

If you don't give an Array to the p-table PrimeNG doesn't generate DOM, it's why nothing is display even if you try to loop on your reports.如果您不向 p 表提供数组 PrimeNG 不会生成 DOM,这就是为什么即使您尝试循环报告也不会显示任何内容的原因。 I think it's more an issue about your use of PrimeNG table我认为这更多是关于您使用 PrimeNG 表的问题

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

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