[igx-list] IgxListItem.index returns wrong index when igx-list is virtualized by igxForOf
Description
IgxListItem.index returns wrong index when igx-list is virtualized by igxForOf.
I have implemented logic in order to get index from clicked item as below.
handleItemClicked(event) {
igxFor.state.startIndex + event.item.index;
}
It worked before, but after updating to v7.2.x, doesn't work.
- igniteui-angular version: 7.2.x
- browser: N/A
Steps to reproduce
- Go to https://stackblitz.com/edit/igx-list-with-igxfor-item-index-issue
- Open console.
- Scroll down to the bottom.
- Click any items.
Result
Wrong indexes are output on console.
Expected result
Until v7.1.x, there was no problem. Please see the same sample on v7.1.12. https://stackblitz.com/edit/igx-list-with-igxfor-item-index
I expect the behavior as v7.1.x.
Attachments
It seem that IgxList.children, which is QueryList<IgxListItem>, is not updated yet when onItemClicked is emitted. I guess IgxListItem.index returns old index at the time.
@tkiryu , this is a side-effect of performance optimizations. We reorder the views but ContentChildren and ViewChildren props don't receive these updates from Angular.
In order to get the correct index you should obtain it from igxFor but igxList was not updated to support this in a meaningful way (i.e. igxListItem doesn't have an index input). This is something I've overlooked because igxList does not use igxFor internally but rather leaves the template to the user.
Once I expose the item index as input, the solution for 7.2 will be something like this:
<igx-list (onItemClicked)="handleItemClicked($event)">
<div ...>
<igx-list-item
...
[index]="i"
*igxFor="let item of data; index as i; scrollOrientation : 'vertical'; containerSize: '480px'; itemSize: '48px'">
<div class="item-container">
...
...
</div>
</igx-list-item>
</div>
</igx-list>
public handleItemClicked(event) {
console.log('Index:', event.item.index);
}