首页 > 解决方案 > 检测角循环中的最后两个元素

问题描述

我在我的模板中使用primeng表,如下所示:

<p-table  [paginator]="true" [rows]="10" [showCurrentPageReport]="true"
          currentPageReportTemplate="Showing {first} to {last} of {totalRecords} entries" [rowsPerPageOptions]="[10,25,50]"
          [value]="histories">
  <ng-template pTemplate="header">
    <tr>
      <th>duration</th>
      <th>build id</th>
    </tr>
  </ng-template>
  <ng-template pTemplate="body" let-build>
    <tr>
      <td [style]="{'word-wrap':'break-word !important'}">
        {{ secondsToDhms(build.duration) }}
      </td>
      <td [style]="{'word-wrap':'break-word !important'}">
        {{ build.build_id }}
      </td>
    </tr>
  </ng-template>
  <ng-template pTemplate="paginatorleft">
    <p-button type="button" icon="pi pi-plus" styleClass="p-button-text"></p-button>
  </ng-template>
  <ng-template pTemplate="paginatorright">
    <p-button type="button" icon="pi pi-cloud" styleClass="p-button-text"></p-button>
  </ng-template>
</p-table>

我如何在 p-table 中获取循环元素的索引,因为它不是经典的 ngfor let of,所以我可以稍后测试它是否是在我的模板中隐藏按钮的最后一个元素?

标签: angularprimeng

解决方案


不妨发布一个正确的答案。

<ng-template pTemplate="body" let-build let-rowIndex="rowIndex">
    <tr>
      <td [style]="{'word-wrap':'break-word !important'}">
        {{ secondsToDhms(build.duration) }}
      </td>
      <td [style]="{'word-wrap':'break-word !important'}">
        {{ build.build_id }}
      </td>
    </tr>
</ng-template>

您可以使用let-rowIndex="rowIndex"它来访问元素的索引。


推荐阅读