Angular 2 - ngFor while index< X

时间:2017-10-11 19:30:08

标签: arrays angular typescript ngfor

我目前正在开发一个带有一系列项目(对象)的角度应用程序。我想在它自己的列表项中显示数组中的每个对象,但是想要在它旁边的新列表上开始之前将列表的高度限制为9行。因此,如果数组有13个元素,则array [0]到array [8]应该列在第一列中的第一列中,而array [9]到array [12]应该列在新列表中。 如何在index = 9处使* ngFor停止循环,并从另一个列表开始循环?

这是我当前代码的外观:

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>

答案 2 :(得分: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>