与 ngx-pagination 一起使用时,ngFor 索引重置为 0

ngFor index reset to 0 when used with ngx-pagination

<tr *ngFor="let data of rawDataListDup | filter:searchText | paginate: { itemsPerPage: 100,currentPage: q }; let idx = index">
<td>
  <select (change)="AssigneeChange(data,$event.target.value,idx)">
   <option [value]="data1.id" *ngFor="let data1 of groupList">{{ data1.name }}</option>
  </select>
</td>
</tr>

rawDataListDup 项视为 300。在上面的代码中 idx 值重置为 0,当我在 table.

中进入下一页时
AssigneeChange(data,id,idx){
  console.log(data,id,idx)
}

rawDataListDup 的当前索引视为 100。在控制台中,当函数 AssigneeChange 被调用时,我得到 idx = 0 而不是 idx = 100。当我在 table 的 page = 2 时会发生这种情况。每页将有 100 个项目。

如何解决这个问题?

<tr *ngFor="let data of rawDataListDup 
   | filter:searchText 
   | paginate: { itemsPerPage: 100,currentPage: q }; 
   let idx = index + paginate.currentPage  * paginate.itemsPerPage;
">

添加索引 pageSizeitemsPerPage 之间的倍数。

示例:

page 0: item 0 = 0 + 0 * 10 = 0
page 0: item 1 = 1 + 0 * 10 = 1
page 1: item 0 = 0 + 1 * 10 = 10
page 1: item 1 = 1 + 1 * 10 = 11

解决办法请参考this..

我们可以使用下面的代码获取绝对索引,因为 paginate 无法识别它。

AssigneeChange(data,id,idx){ 
idx = 100 * (this.q - 1) + idx;
console.log(data,id,idx)
}

将“q”而不是“idx”传递给 AssigneeChange 函数。

解决方法:使用indexOf()方法获取循环中item的索引。

<tr *ngFor="let data of rawDataListDup | filter:searchText | paginate: { itemsPerPage: 100,currentPage: q };>
  <td>
    <select (change)="AssigneeChange(data,$event.target.value, rawDataListDup.indexOf(data)">
      <option [value]="data1.id" *ngFor="let data1 of groupList">{{ data1.name }}</option>
    </select>
  </td>
</tr>