I need the inital position of the cdk-virtual-scroll-viewport
to be other than the first element / item of the list.
Now I found the scrollToIndex
and scrollTo
methods, but I only can get them to work when using it in the ngAfterViewChecked
, which feels off.
ngAfterViewChecked
is the right way of doing things? @ViewChild(CdkVirtualScrollViewport) cdkVirtualScrollViewport: CdkVirtualScrollViewport;
numbers: number[] = [];
constructor() {
for (let index = 0; index < 10000; index++) {
this.numbers.push(index);
}
}
ngAfterViewChecked() {
this.cdkVirtualScrollViewport.scrollToIndex(2000);
}
<ul class="list">
<cdk-virtual-scroll-viewport style="height:264px" itemSize="88">
<ng-container *cdkVirtualFor="let n of numbers">
<li style="height:88px">{{ n }}</li>
</ng-container>
</cdk-virtual-scroll-viewport>
</ul>
I have the same problem, I have a not so elegant solution
contentCheck = 0
ngAfterViewChecked() {
if (this.contentCheck < 3) {
this. cdkVirtualScrollViewport.scrollToIndex(4000);
this.contentCheck++;
}
}
After 3 checks the scrollToIndex works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With