[igxForOf] About scrollPrev() and state.startIndex;
Question
I have an issue with igxForOf.scrollPrev() and igxForOf.state.startIndex.
- igniteui-angular version: 7.2.6
- browser: N/A
I'm implementing a listbox component using igx-list and igxForOf. Here is a sample.
https://stackblitz.com/edit/virtualized-igx-list-scrollprev-issue
We can click an item and move it up/down. But, there is a problem with moving up behavior.
Steps to reproduce
- Scroll down to the bottom.
- Click the last item.
- Click "↑" button 10 times.
Result
Selected item is outside the scroll container.

I call scrollPrev() when selected item is outside the scroll(I determine it based on
igxForOf.state.sartIndex), but it doesn't work as expected only when moving item up from the bottom of the scroll container.
const startIndexInView = this.igxFor.state.startIndex;
if (prevIndex < startIndexInView) {
this.igxFor.scrollPrev();
return;
}
Expected result
Selected item should be in the scroll container.

I found a workaround but I don't want to use it if possible.
Do you know a better way to achieve that? Or is there something wrong with igxForOf behavior?
@tkiryu , try this: https://stackblitz.com/edit/virtualized-igx-list-scrollprev-issue-hfr9qa
igxFor always renders one extra row so that the whole view can be offset and you can see more natural scrolling with partial rows into view on small scroll deltas. When you are at the bottom this extra row is above the row that is the topmost in the view which causes the check to fail.
@ChronosSF , thanks!. I tried it and it looked almost good. But there is one thing that is not as expected. Calling scrollTo() resets scroll position when there is no extra row on the top.
Yeah, there is one scenario that wasn't covered. Unfortunately, the solution doesn't look very pretty but try this:
const sc = this.igxFor.getVerticalScroll();
const isAtBottom = Math.round(sc.scrollTop + parseInt(this.igxFor.igxForContainerSize, 10)) === sc.scrollHeight;
const startIndexInView = this.igxFor.state.startIndex;
if (prevIndex < startIndexInView && !isAtBottom) {
this.igxFor.scrollPrev();
} else if (prevIndex < startIndexInView + 1 && isAtBottom) {
this.igxFor.scrollTo(prevIndex);
}
@ChronosSF , thank you so much! It works well!