2

Working with angular primeng API and following the documentation

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'}
   ]
}

I can render the data successfully using *ngFor

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

Now I need to make modifications on each data value for example:

*name use uppercase pipe
*currency use currency pipe
*date use date 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>

How can I modify data being render and for each value modify accordingly

2
  • I'd look into using a component library that offers a table (like angular material). A better way of doing what you have here, if you want your columns to be dynamic in nature like this, would essentially involve rolling your own component library. Commented May 26, 2020 at 22:08
  • What you could consider using is CDK-table. It has you covered on this. Commented May 26, 2020 at 22:22

3 Answers 3

2

you could make a marginal improvement by using ngSwitch instead:

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

but I'd recommend using some component library that offers a table if you need a table with dynamic columns like this. As your other options would essentially involve rolling your own reusable table component, which may be overkill.

Sign up to request clarification or add additional context in comments.

Comments

0

I hold the same opinion as @bryan60. Material table can save you from such nightmares

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>

Comments

0

Here is in an example I created using CDK-tables:

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

It uses a dynamic approach.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.