lVlyke / lithium-ngx-virtual-scroll

A fast and lightweight virtual scrolling solution for Angular that supports single column lists, grid lists and view caching.
MIT License
23 stars 4 forks source link

No public method to set scroll position #24

Closed ghenry22 closed 1 year ago

ghenry22 commented 1 year ago

The scrollPosition Value is read-only currently.

This means we cannot implement something like a simple scroll to top functionality.

Proposed Solution Implement a "ScrollTo" function which can accept an item number in the virtual list. Implement a "ScrollToXY" function which can accept x/y co-ordinates to set the scrollPosition directly ie scrollToXY({x: 0, y: 0})

lVlyke commented 1 year ago

The component responds to native HTML scroll events, so to update the scroll position you can simply scroll the container element itself. For example:

//MyComponent.ts:
import { ElementRef } from ‘@angular/core’;

class MyComponent {

    @ViewChild("virtualScroll", { read: ElementRef })
    public scrollContainer: ElementRef<HTMLElement>;

    …

    public scrollTo(yPos: number) {
        // li-virtual-scroll will automatically re-render based on the new scroll position
        this.scrollContainer.nativeElement.scrollTo(0, yPos);
    }
}
<!-- MyComponent.html -->
<li-virtual-scroll #virtualScroll [items]="items">
    …
</li-virtual-scroll>
ghenry22 commented 1 year ago

Got it, will give that a try thanks!