2017-10-11 16 views
4

現在、アイテム(オブジェクト)の配列を持つ角型アプリケーションで作業しています。私は、配列内の各オブジェクトを独自のリスト項目に表示したいが、リストの高さを9行に制限して、隣の新しいリストで開始する。したがって、配列に13の要素がある場合、配列[0]から配列[8]は最初の列の最初のリストに、配列[9]から配列[12]は新しいリストにリストされるべきです。 * ngForをindex = 9でループを止めるにはどうしたらいいですか?角度2 - ng For index <x

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

答えて

0

あなたは何ができる何かのように:テンプレートで

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

:これを行うの

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

一つの方法Array.prototype.sliceを使用している

は、これが私の現在のコードがどのように見えるかです方法:

<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

+0

を使用することができます!とても明らかで、しかもスマートです! – ancasen

0

@のyurzuiの回答に加えて、あなたはこれが素晴らしいものである角パイプ

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>