igniteui-angular icon indicating copy to clipboard operation
igniteui-angular copied to clipboard

[igxForOf] About scrollPrev() and state.startIndex;

Open tkiryu opened this issue 7 years ago • 4 comments

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

  1. Scroll down to the bottom.
  2. Click the last item.
  3. Click "↑" button 10 times.

Result

Selected item is outside the scroll container. image

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

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 avatar Apr 24 '19 07:04 tkiryu

@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 avatar Apr 24 '19 11:04 ChronosSF

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

result

tkiryu avatar Apr 24 '19 14:04 tkiryu

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 avatar Apr 25 '19 08:04 ChronosSF

@ChronosSF , thank you so much! It works well!

tkiryu avatar Apr 26 '19 01:04 tkiryu