Angular 2 - ngFor while index < x
Asked Answered
T

3

6

I'm currently working on an angular app with an array of items (objects). I want to display each object in the array in its own list item, but want to limit the height of the list to 9 rows, before it starts on a new list next to it. So if the array has 13 elements, array[0] to array [8] should be listed in the first list, in the first column, while array [9] to array [12] should be listed in a new list. How can I make *ngFor stop looping at index = 9, and start looping from there on another list?

This is how my current code looks:

<div *ngIf="patients" class="col-sm-4" id="patientList">

  <!--Patient 0 to 8 should go in this space-->

  <table id="patientsTab">
    <tr *ngFor="let patient of patients; let i = index;" class="patientRow" >
      <td class="ptName"><button class="patientSelect" (click)="selectPatient(i)" >{{ patient.firstName }} {{ patient.lastName }}</button></td>
      <td class="ptPersonnr">{{ patient.personnr }}</td>
    </tr>
  </table>
</div>

<div *ngIf="patients.length > 9" class="col-sm-4">

  <!--Patient 9 to 13 should go in this space-->

</div>

<div *ngIf="patient" class="col-sm-4">

</div>
Terena answered 11/10, 2017 at 19:30 Comment(0)
C
6

One way of doing this is using Array.prototype.slice method:

<div *ngIf="patients" class="col-sm-4" id="patientList">

  <!--Patient 0 to 8 should go in this space-->

  <table id="patientsTab">
    <tr *ngFor="let patient of patients.slice(0, 8); let i = index;" class="patientRow" >
      <td class="ptName"><button class="patientSelect" (click)="selectPatient(i)" >{{ patient.firstName }} {{ patient.lastName }}</button></td>
      <td class="ptPersonnr">{{ patient.firstName }}</td>
    </tr>
  </table>
</div>

<div *ngIf="patients.length > 9" class="col-sm-4">
  <div *ngFor="let patient of patients.slice(8, 13);">
    {{ patient.firstName }}
  </div>
</div>

Stackblitz Example

Cataract answered 11/10, 2017 at 19:55 Comment(1)
This is great stuff! So obvious, yet so smart!Terena
H
1

In addition to @yurzui's answer, you may use angular pipes

Stackblitz Example

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'stopat'})
export class StopAtPipe implements PipeTransform {
  transform(value: Array<any>, start:number, end:number) {    
    return value.slice(start,end);
  }
}

<div class="container">
  <div class="row">
    <div *ngIf="patients" class="col-sm-4" id="patientList">

      <!--Patient 0 to 8 should go in this space-->

      <table id="patientsTab">
        <tr *ngFor="let patient of patients | stopat:0:8; let i = index;" class="patientRow" >
          <td class="ptName"><button class="patientSelect" (click)="selectPatient(i)" >{{ patient.firstName }} {{ patient.lastName }}</button></td>
          <td class="ptPersonnr">{{ patient.firstName }}</td>
        </tr>
      </table>
    </div>

    <div *ngIf="patients.length > 9" class="col-sm-4">
      <div *ngFor="let patient of patients | stopat:8:13;">
        {{ patient.firstName }}
      </div>
    </div>

    <div *ngIf="patient" class="col-sm-4">

    </div>
  </div>

</div>
Halona answered 11/10, 2017 at 20:28 Comment(0)
T
0

You could do something like:

get slicedList(){
  return this.sliceList(this.patients,9);
}

private sliceList(list: string[], factor: number): string[][] {
    const result: string[][] = [];
    const totalIterations = Math.max(Math.ceil(list.length / factor),1);
    let iteration = 0;

    while (totalIterations > iteration) {
      const start = iteration * factor;
      const end = Math.min((iteration + 1) * factor, list.length);
      result.push(list.slice(start, end));
      iteration++;
    }
    return result;
  }

In template:

<ng-container *ngFor="let sublist of slicedList;index as listIndex">
  // sublist is a list of size N, with N <= 9
  List: {{i+1}}
  <ng-container *ngFor="let patient of sublist; index as patientIndex">
      {{patient | json}}
      <span>Patient at index: {{(listIndex*9)+patientIndex}}</span>
   </ng-container>
</ng-container>
Touzle answered 11/10, 2017 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.