How can I traverse an array with ngFor nested angular 5

0

Surely they will say that it is something very easy to do but I am trying to go through the data array in this way

<tr *ngFor="let list of listadoRangosPivot; let i = index">
    <td class="text-right">{{list.id_tercero_fk}}</td>
    <td>{{list.nombres_tercero}}</td>
    <td *ngFor="let item of listadoRangosPivot; let j = index">{{ list.rangos.total[i,j]}}</td>  
</tr>

This is the JSON with the data I'm trying to cover

But in response this is what it brings to the table, if someone could already do so I would appreciate your help and advice.

    
asked by Jeffry Jose Barrios Gomez 15.05.2018 в 23:38
source

3 answers

0

To solve it, create a pipe and I did it this way

import { PipeTransform, Pipe } from '@angular/core';

@Pipe({ name: 'keys' })
export class KeysPipe implements PipeTransform {
    transform(value, args: string[]): any {
        let keys = [];
        for (let key in value) {
            keys.push({ key: key, value: value[key] });
        }
        return keys;
    }
}

and I had to make these changes in the table

<table id="tableDeudasEdades" name="tableDeudasEdades" class="table" *ngIf="vistaPanelAcitvo==7">
  <thead>
    <tr>
      <th width="150">Identificacion</th>
      <th>Cliente</th>
      <th *ngFor="let item of rangos" class="text-right">DE {{item.i}} A {{item.f}} DIAS</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let keys  of listadoRangosPivot">
      <td>
        <strong>{{keys.id_tercero_fk}}</strong>
      </td>
      <td> {{keys.nombres_tercero}} </td>
      <td *ngFor="let key  of keys.rangos | keys" class="text-right">
        {{key.value | currency:$:'1.0-0'}}
      </td>
      <td></td>
    </tr>
  </tbody>
</table>
    
answered by 17.05.2018 / 20:30
source
0

The best thing you can do is create a computed property and use it in your view to have more maintainable your code.

const data = [
    {
        id_tercero_fk: "1234",
        nombres_tercero: "Maria perez perez",
        rangos: {
            name: [ "DE 1 A 10 DIAS", "DE 11 A 20 DIAS", "DE 21 A 20 DIAS" ],
            total: [ 4000000, 4000000, 0 ]
        }
    }
]

const items = data.map(item => ({
    id: item.id_tercero_fk || 0,
    customer: item.nombres_tercero || '',
    from1To10Days: item.rangos.total[0] || 0,
    from11To20Days: item.rangos.total[1] || 0,
    from21To20Days: item.rangos.total[2] || 0
}));

<tr *ngFor="let item of items; let i = index">
  <td [class.text-right]="i === 0">{{ item }}</td>
</tr>
    
answered by 16.05.2018 в 00:14
0

It would be something like this:

<tr *ngFor="let list of listadoRangosPivot; let i = index">
<td class="text-right">{{list.id_tercero_fk}}</td>
<td>{{list.nombres_tercero}}</td>
<td *ngFor="let item of list.rangos; let j = index">{{ item.total[0]}}</td>

    
answered by 16.05.2018 в 00:18