I'm very new to angular and material and having a hard problem of disabling the non-selected tabs in angular 4 material and I have only this code below.
<md-tab-group class="flex-stretch tab-button-arrows">
<md-tab *ngFor="let subject of subjects" label="subject.name" ></md-tab>
</md-tab-group>
Note: The subjects is a dynamic array.
The solution 🕶 First, we need to add a click listener on the mat-tab-group and disable all tabs because if tabs are not disabled then the user will be able to navigate on them and we want to navigate on tabs conditionally not manually.
I found the solution. Use ng-disabled = "true" on <md-tab> from the beginning and enable it when the user clicks on the next button. Show activity on this post. Use md-no-select-click attribute on md-tabs to achieve the result.
You can use selectedIndex property to set the default tab on the tab group. Setting selectedIndex property as 0 sets the first tab and setting it as 1 sets the second tab.
Angular Material Tabs | Learn How to Create Tabs in Angular Material? In Angular material, tabs are present to show the content into different sections. These tabs increase the readability of the user and make the application more user interactive.
Changing a tab header position is effortless in angular material you have to define the headerPosition property with the mat-tab-group directive as shown in below example: The selectedIndex property sets the active tab; it works on the index number pattern. It needs to be added to the mat-tab-group element.
The selectedIndex property sets the active tab; it works on the index number pattern. It needs to be added to the mat-tab-group element. In the following example, we have declared the active variable with numeric value 0 in the angular template. You have to define the active tab index variable in the angular TypeScript template:
The mat-tab-group directive is a wrapper around mat-tab directive; the mat-tab is the panel which forms the angular material tab. The following code invokes the simple material tab with four panels. Open app.component.html template and insert the below code, however you can place the tab code in any angular HTML template:
All you need to do is use default property of mat-tab
isActive
: ReadMore
<mat-tab-group>
<mat-tab #tab [disabled]='!tab.isActive' *ngFor="let subject of subjects" [label]="subject.name">
{{ subject.name }}
</mat-tab>
</mat-tab-group>
WORKING DEMO
You can add a [disabled] tag to your mat-tab, with a function linked to it. And have a index for each tab. Something like this:
<md-tab-group class="flex-stretch tab-button-arrows" [selectedIndex]="selectedIndex">
<md-tab [disabled]="isSelected(i)" *ngFor="let subject of subjects; let i = index" label="subject.name" ></md-tab>
</md-tab-group>
Then you declare the function on your component to disable if true:
isSelected(index: number) {
if (this.selectedIndex == index) {
return false;
} else {
return true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With