components icon indicating copy to clipboard operation
components copied to clipboard

mat-table with sticky header doesn't work in mat-tab-group

Open benneq opened this issue 7 years ago • 12 comments

Reproduction

Stackblitz Link: https://stackblitz.com/edit/components-issue-any3dl

Steps to reproduce:

  1. Put a mat-table inside a mat-tab-group and set sticky: true
  2. scroll down

Expected Behavior

Table header should be sticky

Actual Behavior

Table header is not sticky

Environment

  • Angular: 8.0.0
  • CDK/Material: 8.0.1
  • Browser(s): Chrome 75
  • Operating System (e.g. Windows, macOS, Ubuntu): macOS Mojave 10.14.5

benneq avatar Jun 12 '19 23:06 benneq

That's because the scroll now happens on the tab itself rather than the table. You need to make sure the tab has a height: 100% (or some defined height) so that the table can take over the scroll.

I don't think there's anything Angular Components can do, as it's a case by case decision whether the tab or its content should act as the scroll container.

Airblader avatar Jun 13 '19 06:06 Airblader

Can you provide an example? I tried setting height: 100% at several places, but the header still isn't sticky.

Bonus question: When is it useful to have either tab or content as scroll container?

benneq avatar Jun 13 '19 07:06 benneq

I'm on mobile so I can't dig into the example, sorry. It may well be that the structure of Material tabs currently don't allow setting it at the correct place, in that case that is something that should be enabled.

Bonus question: When is it useful to have either tab or content as scroll container?

I wasn't precise enough above. The tab never scrolls right now, it just doesn't have a defined height so it expands to fit its content. With the sticky header table the problem is that the table needs some defined height, otherwise the header never has a reason to stick.

The only two cases where it makes sense to use this 100% height thing that I can think of are some sticky content in the tab (header or footer) or when wanting to vertically center something in the tab (as vertically centering in a parent that is fit to content doesn't make sense).

Airblader avatar Jun 13 '19 07:06 Airblader

Note that 100% height isn't always the correct answer. The tab itself of course has a parent as well and it somewhat depends on the layout you're talking about. For example if above the tab you have some text, then 100% height on the tab would cause page scroll (and then table scroll inside the tab, so two scroll bars) when likely you'd want to have the text and tab combined to be 100% viewport height.

Airblader avatar Jun 13 '19 07:06 Airblader

I have 2 nearly identical views within a <router-outlet>. The first one is: <mat-toolbar></mat-toolbar><mat-table></mat-table> And the second one is: <mat-toolbar></mat-toolbar><mat-tab-group><mat-table></mat-table></mat-tab-group> The first one works with sticky header, and the second one doesn't. The only difference is the tabs component. Though there isn't much "parent" available. When I put the table below the the tabs component, it works perfectly.

Maybe you got some time in the next days to help me. I'm clueless...

benneq avatar Jun 13 '19 08:06 benneq

Setting the height of mat-tab-group to 100vh seems to solve your problem.

<mat-tab-group style="height: 100vh">

jymdman avatar Jun 13 '19 18:06 jymdman

@jymdman Not really. This only works, if there's nothing above the tabs. I've edited the Stackblitz example. It now has a toolbar above the tabs, and a card below. If you remove the Tabs and just keep toolbar, table, card you can see what behavior I want (just with the additional tabs)

benneq avatar Jun 14 '19 06:06 benneq

I know found the "issue". There are lot's of overflow: hidden on mat-tab-body-*

Here's what I need to do:

  • mat-tab-body-wrapper
  • mat-tab-body-active
  • mat-tab-body-content On all CSS classes set overflow: initial or overflow: visible

But view encapsulation is getting in the way. It only works when defining those styles globally in styles.scss. I now use a trigger CSS class to apply my custom styles:

.tab-custom-overflow .mat-tab-body-wrapper {
    overflow: visible !important;
}
.tab-custom-overflow .mat-tab-body-active {
    overflow: visible !important;
}
.tab-custom-overflow .mat-tab-body-content {
    overflow: visible !important;
}

And then use it in the template:

<mat-tab-group class="tab-custom-overflow">
  ...
</mat-tab-group>

But now I can't get horizontal scrolling for the table working...

benneq avatar Jun 15 '19 01:06 benneq

Also, the animation while changing tabs isn't very pleasing. Do you have any progress on this issue?

daniel-dimitrovv avatar Aug 26 '19 06:08 daniel-dimitrovv

The @benneq answer worked for me, thanks man ! I declared the rules directly in my component, to avoid adding a class in styles.scss. Therefore I had to add the ::ng-deep prefix to the selector in order to make it work.

Drillman avatar Mar 12 '20 15:03 Drillman

This seems to be an issue with layout and styles that can be resolved with overrides. I dont believe there is any action we can take here

andrewseguin avatar May 28 '20 17:05 andrewseguin

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.