Search code examples
javascriptangularangular-materialmat-table

Angular updating matTableDatasource with interval brings scroll to the top of page


I have a mat-table which needs to be updated about the changes. I set an interval and inside the interval I call a method which is like

updateMatTable(offset: number, size: number): void {

        offset = offset * size;

        /* GET DATA FROM REST SERVICE */

            this.dataSource = new MatTableDataSource(this.newTableData.dataRows);

        });

    }

it seems like equating dataSource to the newTableData brings the scroll position to top of the page only when we are at a scroll position where we can see under the mat-table (near bottom of page ) and that the table needs scrolling down

Is this a bug about the library or am I missing something about mat-tables.

Thank you for the answers


Solution

  • Ok,I added a change detector under the codes and it is not behaving strange somehow. I would like to have an explanation tho.

        updateMatTable(offset: number, size: number): void {
        
                offset = offset * size;
        
                /* GET DATA FROM REST SERVICE */
        
                    this.dataSource = new MatTableDataSource(this.newTableData.dataRows);
    
                    this.changeDetectorRef.detectChanges();
        
                });
        
            }