繁体   English   中英

Angular *ngFor 指令在视图上显示数据

[英]Angular *ngFor directive display data on view

使用 angular primeng API 并遵循文档

API [
      id: 1, name:"Test 1", currency: "Euro", status: "Active", date: "4th of April 2008" },
      id: 2, name:"Test 2", currency: "Dollar", status: "Active", date: "12nd of May 2005" }
   ]

 ngOnInit {
   this.col = [
     {field: 'name', header: 'Name'},
     {field: 'currency',  header: 'Currency'},
     {field: 'status', header: 'Status'},
     {field: 'date', header: 'Date'}
   ]
}

我可以使用 *ngFor 成功渲染数据

Template.html
<tr>
  <td *ngFor="let col of column">
      {{rowData[col.field]}}
  </td>
<tr>

现在我需要对每个数据值进行修改,例如:

*名称使用大写 pipe
*货币使用货币 pipe
*日期使用日期 pipe

<td *ngFor="let col of columne">
   <ng-container *ngIf="col.field === 'date'"> {{rowData[col.field] | date: 'dd/MMM/yyyy'}}</ng-container>
   <ng-container *ngIf="col.field === 'currency'"> {{rowData[col.field] | currency:'CAD':'code'}}</ng-container>
   <ng-container *ngIf="col.field === 'name'"> {{rowData[col.field] | uppercase}}</ng-container>
</td>

如何修改正在呈现的数据并为每个值进行相应修改

您可以改用ngSwitch进行边际改进:

  <ng-container *ngFor="let col of columne">
    <td [ngSwitch]="col.field">
       <ng-container *ngSwitchCase="'date'"> {{rowData[col.field] | date: 'dd/MMM/yyyy'}}</ng-container>
       <ng-container *ngSwitchCase="'currency'"> {{rowData[col.field] | currency:'CAD':'code'}}</ng-container>
       <ng-container *ngSwitchCase="'name'"> {{rowData[col.field] | uppercase}}</ng-container>
       <ng-container *ngSwitchDefault>{{rowData[col.field]}}</ng-container
    </td>
  </ng-container>

但是,如果您需要具有此类动态列的表,我建议您使用一些提供表的组件库。 因为您的其他选择基本上涉及滚动您自己的可重用表组件,这可能是矫枉过正。

我和@bryan60 持有相同的观点。 材料表可以让你摆脱这样的噩梦

const dataSource = [
  {id: 1, name:"Test 1", currency: "Euro", status: "Active", date: "4th of April 2008" },
  {id: 2, name:"Test 2", currency: "Dollar", status: "Active", date: "12nd of May 2005" }
];
displayedColumns: string[] = ['id', 'name', 'currency', 'status', 'date'];
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <ng-container matColumnDef="id">
    <th mat-header-cell *matHeaderCellDef> Id </th>
    <td mat-cell *matCellDef="let element"> {{element.id}} </td>
  </ng-container>

  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef> Name </th>
    <td mat-cell *matCellDef="let element"> {{element.name}} </td>
  </ng-container>

  <ng-container matColumnDef="currency">
    <th mat-header-cell *matHeaderCellDef> Currency </th>
    <td mat-cell *matCellDef="let element"> {{1 | currency: element.currency}} </td>
  </ng-container>

  <ng-container matColumnDef="status">
    <th mat-header-cell *matHeaderCellDef> Status </th>
    <td mat-cell *matCellDef="let element"> {{element.status}} </td>
  </ng-container>

  <ng-container matColumnDef="date">
    <th mat-header-cell *matHeaderCellDef> Date</th>
    <td mat-cell *matCellDef="let element"> {{element.date | date: 'dd/MM/yyyy' }} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

这是我使用 CDK 表创建的示例:

https://stackblitz.com/edit/angular-ivy-bes8zf

它使用动态方法。

暂无
暂无

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

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