[英]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
.添加以索引
pageSize
和itemsPerPage
之间的倍数。
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
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.