【问题标题】:how to scroll to top in material table on paginate change如何在分页更改时滚动到材料表的顶部
【发布时间】:2019-08-04 01:33:26
【问题描述】:

我遇到了一个问题,在材料表中,我点击下一步按钮,滚动停留在底部,而它应该滚动到顶部。

<mat-table class="table-container" fxFlex="100" [dataSource]="allUsersDataSource">
  <ng-container matColumnDef="firstName">
    <mat-header-cell fxFlex="25" *matHeaderCellDef> First Name </mat-header-cell>
    <mat-cell fxFlex="25" *matCellDef="let element"> {{element.first_name}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="lastName">
    <mat-header-cell fxFlex="25" *matHeaderCellDef> Last Name </mat-header-cell>
    <mat-cell fxFlex="25" *matCellDef="let element"> {{element.last_name}} </mat-cell>
  </ng-container> 
     
  <mat-header-row *matHeaderRowDef="allUsersColumns; sticky: true"></mat-header-row>
  <mat-row *matRowDef="let row; columns: allUsersColumns;"></mat-row>
</mat-table>

<mat-paginator #allUsersPaginator [pageSizeOptions]="[25,50,100]" (page)="onAllUserPaginateChange($event)"
    [length]="allUserPagination.length" [pageSize]="25" showFirstLastButtons></mat-paginator>

这是后面的代码

onAllUserPaginateChange(event: any) {
  window.scroll(0,0);
}

【问题讨论】:

    标签: angular pagination angular-material


    【解决方案1】:

    这里我给了桌子一个ID

    例如。

    <mat-table id ="matTable" class="table-container" 
      [dataSource]="allUsersDataSource">
    <mat-table>
    

    在打字稿文件中该表的分页方法中。

    onAllUserPaginateChange(event: any) {
      const matTable= document.getElementById('matTable');
      matTable.scrollTop = 0;
    }
    

    【讨论】:

    • 使用 Angular7,.scrollTop = 0; 对我不起作用,但 .scrollIntoView(); 确实...fwiw,不使用&lt;mat-table...&gt;,而是使用具有mat-table 属性的常规&lt;table...&gt; 元素。
    【解决方案2】:

    在 ts 文件中使用以下分页 onchange 函数

    const peopleIcanTop = document.getElementById('peopleIcanTop');
    peopleIcanTop.scrollIntoView();
    

    【讨论】:

      猜你喜欢
      • 2022-07-08
      • 2020-08-08
      • 2022-12-31
      • 2022-01-21
      • 1970-01-01
      • 2021-07-28
      • 2020-05-10
      • 1970-01-01
      • 2022-08-24
      相关资源
      最近更新 更多