2

Is there a way I can have a material table with a sticky header and the columns responsive? I found this example on material documentation: https://stackblitz.com/angular/paddnlmnavx?file=src%2Fapp%2Ftable-sticky-complex-flex-example.css

My issue here is that the header width is fixed, calculated using the cell's width:

min-width: 1920px; /* 24 columns, 80px each */

If I want to make the columns responsive then I will have a dynamic width for the columns and if I remove the min-width from the header, the header row will have a width of 100% and will not contain all the header cells.

2
  • 1
    What exactly do you need?? Commented May 17, 2021 at 10:07
  • 1
    Can you elaborate what you want to achieve? Commented May 17, 2021 at 10:22

3 Answers 3

1

One way to solve this is with @media query. You can change the min-width based on screen size and you will have responsive columns.

You can do it like this:

.mat-header-row,
.mat-footer-row,
.mat-row {
  min-width: 1920px;
}

@media (max-width: 1200px) {
  .mat-header-row,
  .mat-footer-row,
  .mat-row {
    min-width: 2500px;
  }
}

@media (max-width: 992px) {
  .mat-header-row,
  .mat-footer-row,
  .mat-row {
    min-width: 3000px;
  }
}

@media (max-width: 768px) {
  .mat-header-row,
  .mat-footer-row,
  .mat-row {
    min-width: 4000px;
  }
}

Here is the updated example: https://stackblitz.com/edit/angular-k6repx-qpnjoa?file=src/app/table-sticky-complex-flex-example.css

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

Comments

1
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true">

You can find more details on this page

Comments

0

Try to use this code structure to make angular material table header as sticky.

<tr mat-header-row *matHeaderRowDef="headerdata" sticky></tr>

Adding sticky keyword to the <tr mat-header-row> tag. And this change will work in angular 6.2.3+ version.

Comments

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.