简体   繁体   English

Angular材料表带粘header

[英]Angular material table with sticky header

I'm using angular7 with Angular material design templates.我将 angular7 与 Angular 材料设计模板一起使用。 Currently, my result is as follows,目前,我的结果如下, 在此处输入图像描述

But I want my result is as follows.但我希望我的结果如下。 Scrollbar under sticky header.粘性 header 下的滚动条。

在此处输入图像描述

I put some demo code and live demo as your reference.我放了一些演示代码和现场演示作为您的参考。

HTML HTML

<div class="example-container mat-elevation-z8">
  <table mat-table [dataSource]="dataSource">

    <!-- Position Column -->
    <ng-container matColumnDef="position">
      <th mat-header-cell *matHeaderCellDef> No. </th>
      <td mat-cell *matCellDef="let element"> {{element.position}} </td>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef> Name </th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>

    <!-- Weight Column -->
    <ng-container matColumnDef="weight">
      <th mat-header-cell *matHeaderCellDef> Weight </th>
      <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
    </ng-container>

    <!-- Symbol Column -->
    <ng-container matColumnDef="symbol">
      <th mat-header-cell *matHeaderCellDef> Symbol </th>
      <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>
</div>

CSS CSS

.example-container {
  height: 400px;
  overflow: auto;
}

table {
  width: 100%;
}

You can do something like that using two table one for the header and another for the data.您可以使用两个表来执行类似的操作,其中一个用于 header,另一个用于数据。 Please see the working demo .请参阅工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM