0

I'm using angular7 with Angular material design templates. Currently, my result is as follows, enter image description here

But I want my result is as follows. Scrollbar under sticky header.

enter image description here

I put some demo code and live demo as your reference.

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

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

table {
  width: 100%;
}
1
  • 2
    Its the default behavior of angular material tables. If you want scroll out side you need to make two separate tables or somehow you able to make scroll inside then your header and body cell aliment create problem Commented Oct 30, 2019 at 5:54

1 Answer 1

1

You can do something like that using two table one for the header and another for the data. Please see the working demo.

Sign up to request clarification or add additional context in comments.

1 Comment

Be aware that this solution causes the headers to misalign, because the scrollbar doesn't take up space there.

Your Answer

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.