angular / components

Component infrastructure and Material Design components for Angular
https://material.angular.io
MIT License
24.32k stars 6.73k forks source link

bug(virtual-scroll): scrollToIndex does not work for multiple viewports scrolling over the same element #28469

Open RobinBomkamp opened 7 months ago

RobinBomkamp commented 7 months ago

Is this a regression?

The previous version in which this bug was not present was

No response

Description

If you have multiple viewports beneath eachother the function 'scrollToIndex' does not work if you want to scroll to an element in one of the bottom lists. It scrolls to a wrong position.

Reproduction

StackBlitz link: https://stackblitz.com/edit/components-issue-dch1m4?file=src%2Fapp%2Fexample-component.html,src%2Fapp%2Fexample-component.ts,src%2Fapp%2Fexample-component.css Steps to reproduce:

  1. Click the button 'Scroll to viewport 3 and index 7'
  2. It scrolls to the wrong position.

Expected Behavior

It should scroll to the correct position.

Actual Behavior

It should scroll to the wrong position.

Environment

RobinBomkamp commented 7 months ago

Still does not work for me. If you have other elements in the same scrolling element you are still scrolling to the wrong location.

Harpush commented 3 months ago

It seems to be broken indeed but the fix is simple:

this.viewport.scrollable.scrollTo({
  top: this.viewport.measureViewportOffset('top') + index * itemHeight
})
RobinBomkamp commented 2 months ago

It seems to be broken indeed but the fix is simple:

this.viewport.scrollable.scrollTo({
  top: this.viewport.measureViewportOffset('top') + index * itemHeight
})

Thanks for the suggestion, but it does not work if there is a headline above every list in the viewport.

Harpush commented 2 months ago

It seems to be broken indeed but the fix is simple:

this.viewport.scrollable.scrollTo({
  top: this.viewport.measureViewportOffset('top') + index * itemHeight
})

Thanks for the suggestion, but it does not work if there is a headline above every list in the viewport.

Mind sharing a stackblitz? Works fine for me with headers

RobinBomkamp commented 2 months ago

It seems to be broken indeed but the fix is simple:

this.viewport.scrollable.scrollTo({
  top: this.viewport.measureViewportOffset('top') + index * itemHeight
})

Thanks for the suggestion, but it does not work if there is a headline above every list in the viewport.

Mind sharing a stackblitz? Works fine for me with headers

Sorry, I had an error in my code. It seems to work. Thank you. :)