Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable all tabs except selected tab in angular4 material

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.

like image 857
DaLoco Avatar asked Jan 11 '18 12:01

DaLoco


People also ask

How do you conditionally prevent users from navigating to other tab in Mat tab group?

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.

How do I disable MD tabs?

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.

How do I select a tab in angular materials?

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.

What are tabs in Angular Material?

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.

How to change a tab header position in Angular Material?

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.

How to set the active tab index in angular typescript?

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:

What is mat-Tab-group directive in angular?

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:


2 Answers

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

like image 53
Vivek Doshi Avatar answered Sep 30 '22 00:09

Vivek Doshi


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;
        }
    }
like image 31
Mjstk Avatar answered Sep 30 '22 01:09

Mjstk