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>
Use ngFor and ngIf on same element It's very common scenario where we want to repeat a block of HTML using ngFor only when a particular condition is true. i.e., if ngIf is true.So in this case to use *ngIf and *ngFor on same element, place the *ngIf on a parent element that wraps the *ngFor element.
We use the NgFor directive to loop over an array of items and create multiple elements dynamically from a template element. The template element is the element the directive is attached to. We can nest muliple NgFor directives together.
Declare a variable inside *ngFor directive using let or as keyword. for instance say indexofelement or simply i . Assign the variable value to index .
ngForOf: NgIterable<T> : The value of the iterable expression. Useful when the expression is more complex then a property access, for example when using the async pipe ( userStreams | async ). index: number : The index of the current item in the iterable.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With