简体   繁体   English

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

Consider rawDataListDup items as 300 . rawDataListDup项目视为300 In above code idx value reset to 0, when ever I go to next page in table.在上面的代码中, idx值重置为 0,当我 go 到表中的下一页时。

AssigneeChange(data,id,idx){
  console.log(data,id,idx)
}

Consider the current index of rawDataListDup as 100 .rawDataListDup的当前索引视为100 In the console i get idx = 0 instead of idx = 100 when function AssigneeChange is called.在控制台中,当调用 function AssigneeChange时,我得到idx = 0而不是idx = 100 This happens when i am in page = 2 of table.当我在表格的page = 2时会发生这种情况。 In every page there will be 100 items.每页将有 100 个项目。

How to solve this problem?如何解决这个问题呢?

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

add to index the multiple between pageSize and itemsPerPage .添加以索引pageSizeitemsPerPage之间的倍数。

example:例子:

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

Please refer this for solution..请参考解决方案..

We can get absolute index using below code since paginate doesn't recognise it.我们可以使用下面的代码获得绝对索引,因为分页无法识别它。

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

pass "q" instead of "idx" to AssigneeChange function.将“q”而不是“idx”传递给 AssigneeChange function。

Solution: Using indexOf() method to get index of item in the loop.解决方案:使用indexOf()方法获取循环中项目的索引。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM