0

I have a class with 3 objects in a class that I push in an array and I have to display them grouped in an array like below.

Each line has to be an object. The problem is that I can't find a way to display them like this. I don't use angular material.

Product Price Tax
P1 2 1
P2 1 2
P3 3 3
----- ----- -----
P1 15 4
P2 10 5
P3 20 6

the content of the array:

[
    {
    store: {product_from_store:P1, price:2, tax:1},
    client: {product_from_client:P2, price:1, tax:2},
    sub: {product_from_sub:P3, price:3, tax:3}
    },
 {
    store: {product_from_store:P1, price:15, tax:4},
    client: {product_from_client:P2, price:10, tax:5},
    sub: {product_from_sub:P3, price:20, tax:6}
    }
]

I build this array with different objects so I can't make a simple *ngFor to display them

I think something like this can work but I don't get the expected result.

   <tr *ngFor="let product of groupedTable; let i = index">
        <td>
          <ng-container
            *ngTemplateOutlet="itemTemplate;">
          </ng-container>
        </td>
      </tr>
      <ng-template #itemTemplate>
       //row content ?
    </ng-template>
leri
  • 261
  • 1
  • 3
  • 12

1 Answers1

0

You can use ng-container to loop over the groups, and then loop over each record in the tr like this maybe?

<ng-container *ngFor="let group of groups">
    <tr *ngFor="let record of group">
        <td *ngFor="let column of record | keyvalue">
            {{ column.key }} {{ column.value }}
        </td>
    </tr>
</ng-container>
Mathew Berg
  • 28,625
  • 11
  • 69
  • 90
  • I believe I can't loop with objects inside `Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.` I will try to make child arrays instead of objects – leri May 03 '22 at 15:56
  • 1
    Ah, ok so for that you'd have to use the keyvalue pipe: https://angular.io/api/common/KeyValuePipe – Mathew Berg May 03 '22 at 15:57