Open mahivitsit opened 5 years ago
Please provide this feature ASAP.
Why don't simply use an *ngIf
?
Why don't simply use an
*ngIf
?
if i use *ngIf it doesn't work for
As mentioned above, you should be able to include or remove a tab using ngIf
can you expand on a case where this does not work?
I would love to see this feature to. Because when I use ngIf, it disapears from the DOM, and in the case I use a navigate to tab in case the user click on the button, when using more than 1 tab, the navigation won't work like expected, because ngIf had the tabs removed.
In my case I'm using the tabs as update forms. They are displayed only if the user clicks on the update button. But when I'm having two different sort buttons that are linked to an index of the tab, then the setValue for the selected tab won't work correctly, it will indeed swipe to the tab with the index, but because the other tabs are removed from the DOM, they won't count. I use workarounds to navigate to the wanted tabs, but that's a lot of if and else for the achievement.
I faced the same problem on my Angular project. I also used the material tabs and if I tried to hide a tab using *ngIf.
In this small test project I just tried to figure the problem out: https://stackblitz.com/edit/angular-pbybg8 It will show correctly only one tab (because the checkbox is unchecked and so the result inside ngIf is false). But if i now check the checkbox the result inside ngIf will be true but the tab won't appear. If you invert the "load result" in *ngIf to true it will appear at start but if you uncheck the checkbox it won't disappear. Funny side note: If you add an event handler like (click) or (change) to the checkbox everything works fine! But in my case this won't solve the problem.
Hi, Wel actually I solved it with a switch case that goes through the different possibilities. It will calculate the index for the tab that's shown. It's really hacky but it worked for my case.
Télécharger Outlook pour Androidhttps://aka.ms/ghei36
From: florianraffl notifications@github.com
Sent: Wednesday, March 6, 2019 5:30:59 PM
To: angular/material2
Cc: LauraStordeur; Comment
Subject: Re: [angular/material2]
I faced the same problem on my Angular project. I also used the material tabs and if I tried to hide a tab using *ngIf.
In this small test project I just tried to figure the problem out: https://stackblitz.com/edit/angular-pbybg8 It will show correctly only one tab (because the checkbox is unchecked and so the result inside ngIf is false). But if i now check the checkbox the result inside ngIf will be true but the tab won't appear. If you invert the "load result" in *ngIf to true it will appear at start but if you uncheck the checkbox it won't disappear. Funny side note: If you add an event handler like (click) or (change) to the checkbox everything works fine! But in my case this won't solve the problem.
— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/angular/material2/issues/14227#issuecomment-470176505, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AqDiAp9GPNjy9UqJsvoT_LG3LpPxhtmLks5vT-1DgaJpZM4YtJHv.
Hi, that won't work in my case because i need more tabs with different conditions. But thanks for your answer :)
Another reason for having this feature is that ngIf loads the tab on demand so in my case I have a set of expansion panels in the "hidden" tab that load expanded in the tab switch animation and then collapse again once finished. If there was a hide/show feature, the tab would be loaded when the tab set loads and showing it would not go through this loading process. I have tried setting the hidden attribute but that doesn't seem to affect mat tabs.
I too, would like to have this feature.
any update on this feature!
This worked well for me:
<mat-tab-group
(selectedTabChange)="selectTab($event)"
[selectedIndex]="activeTab"
[ngClass]="{'class-hide-tab0': hideTab0Flag}">
<mat-tab label="Tab0"></mat-tab>
<mat-tab label="Tab1"></mat-tab>
<mat-tab label="Tab2"></mat-tab>
</mat-tab-group>
with CSS:
.class-hide-tab0 div.mat-tab-label:nth-child(1) {
display: none;
}
This preserved the indexing structure underneath, so that I could still reference the same tab by either index or name.
"hideTab0Flag" In which event we need to update this flag ? Please share more info on this flag.
"hideTab0Flag" In which event we need to update this flag ? Please share more info on this flag.
That depends on when you want to show or hide the tab right?
"hideTab0Flag" In which event we need to update this flag ? Please share more info on this flag.
That's just a boolean variable from the code behind. You set it whenever/wherever you want, or you could even add a function to return a boolean true/false.
I had a lot of tabs to display / hide, so creating a CSS class for each tab wasn't the best option for me.
Also, I wanted to preserve the index order to navigate between tabs. This worked for me using js:
displayOrHideTab(tabIndex: number, displayTab: boolean): void {
const tabHeaders = document.querySelectorAll('.mat-tab-label');
if (tabHeaders[tabIndex]) {
(tabHeaders[tabIndex] as HTMLElement).style.display = displayTab ? 'inherit' : 'none';
}
}
And then, to hide any tab:
this.displayOrHideTab(3, false);
Keep in mind to call this function not before ngAfterViewInit()
, as the tabs are not created yet.
Remember to +1 (thumbs up) the original post as it can be used for sorting important/popular feature requests.
It would be nice if even classes/ngClass applied to the tab/header that were added so we could achieve this ourselves:
<mat-tab [class.hidden]="tab.hidden" *ngFor="let tab of tabs">
...
</mat-tab
I've found a hack which allows this without JS:
<mat-tab *ngFor="let tab of tabs" [aria-labelledby]="tab.hidden ? 'hidden' : ''">
...
</mat-tab>
.mat-tab-label[aria-labelledby='hidden'] {
display: none;
}
Thanks you @intellix for your solution. But I changed the css
.mat-tab-label[aria-labelledby='hidden'] { //display: none; width: 0px !important; min-width: 0px; padding: #0px; }
All, I've found a solution that actually works - it uses *ngTemplateOutlet to wrap the same code in a mat tab if the length is > 1. If the length of your data is 1 it makes no sense to have there be a visible mat tab, so this fixes that:
`
<ng-container *ngIf="this.yourData.length > 1">
<mat-tab-group dynamicHeight animationDuration="0ms" disableRipple="true">
<ng-container *ngFor="let item of yourData;">
<mat-tab label="{{item.someAttribute}}">
<ng-container *ngTemplateOutlet="CommonCode"></ng-container>
</mat-tab>
</ng-container>
</mat-tab-group>
</ng-container>
<ng-container *ngIf="this.yourData.length == 1">
<ng-container *ngFor="let item of yourData;">
<ng-container *ngTemplateOutlet="CommonCode"></ng-container>
</ng-container>
</ng-container>
<ng-template #CommonCode>
YOUR CODE HERE
</ng-template>
`
Bug, feature request, or proposal: