0

I have a mat-table in my app (angular v20) that is loading a longer list. If an additional ID is provided, I would like to scroll to that ID in the table.

My problem is that I didn't find the right event to do this on page load. I have a button that can call my scrollToRow(id) method - this works nicely.

I tried to call the scrollToRow in ngAfterViewInit() - didn't work. changeDetection: ChangeDetectionStrategy.Default, not using signals. matTable's datasource is refreshed in ngOnInit.

<table style="width: 100%;" mat-table matTableResponsive matSort [dataSource]="dataSource" (matSortChange)="announceSortChange($event)" >

Do you have an idea how this issue can be fixed?

asked Sep 15 at 21:21
1
  • I found my issue but Mario's comment lead to the real hint: stackoverflow.com/questions/59382950/… In my case, I also forgot to add the: export class ListComponent implements OnInit, ***AfterViewInit*** Commented Sep 17 at 13:41

1 Answer 1

1

This issue may happen because the table rows aren’t fully rendered yet when ngAfterViewInit runs.
A quick test to confirm is wrapping your scroll call in a setTimeout

ngAfterViewInit() {
 if (this.initialRowId) {
 setTimeout(() => this.scrollToRow(this.initialRowId));
 }
}
answered Sep 17 at 12:00
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Through your hint, I found it ;)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.