Programmatically select mat-tab in Angular 2 material using mat-tab-group

前端 未结 4 1781
误落风尘
误落风尘 2020-12-13 23:53

How can I select a specific tab when an event occurs?

I tried with [selectedIndex]=\"selectedTab\" changing the selectedTab to the tab ind

相关标签:
4条回答
  • 2020-12-14 00:15

    I had the same issue and I tried the above answers but they are not helping. Here is my solution:

    In my typescript code, first, declare a variable:

    selected = new FormControl(0); // define a FormControl with value 0. Value means index.
    

    then, in the function:

    changeTab() {
        this.selected.setValue(this.selected.value+1);
     } //
    

    in the html,

     <mat-tab-group [selectedIndex]="selected.value" (selectedIndexChange)="selected.setValue($event)">
                <mat-tab label="label0">0</mat-tab>
                <mat-tab label="label1">1</mat-tab>
                <mat-tab label="label2">2</mat-tab>
                <mat-tab label="label3">3</mat-tab>
                <mat-tab label="label4">4</mat-tab>
                <mat-tab label="label5">5</mat-tab>
    </mat-tab-group>
    
    <button (click)="changeTab()">ChangeTab</button>
    
    0 讨论(0)
  • 2020-12-14 00:36

    I also had similar issue. In my case I needed to show the tab the user was there before he left the component. I solved this by stuffing the current selected tab index in a service.

    On HTML template I have this:

    <mat-tab-group [selectedIndex]="getSelectedIndex()" (selectedTabChange)="onTabChange($event)">
    

    Implementation of onTabChange and getSelectedIndex are as follows:

        getSelectedIndex(): number {
            return this.appService.currentTabIndex
        }
    
        onTabChange(event: MatTabChangeEvent) {
            this.appService.currentTabIndex = event.index
        }
    

    My service code looks like this:

    export class AppService {
        public currentTabIndex = 1  //default tab index is 1
    }
    
    0 讨论(0)
  • 2020-12-14 00:39

    UPDATE (using newest angular+material)

    there are multiple ways..

    1. possible solution, using two-way databinding
    <button mat-raised-button (click)="demo1BtnClick()">Tab Demo 1!</button>
    <mat-tab-group [(selectedIndex)]="demo1TabIndex">
        <mat-tab label="Tab 1">Content 1</mat-tab>
        <mat-tab label="Tab 2">Content 2</mat-tab>
        <mat-tab label="Tab 3">Content 3</mat-tab>
    </mat-tab-group>
    
    public demo1TabIndex = 1;
    public demo1BtnClick() {
      const tabCount = 3;
      this.demo1TabIndex = (this.demo1TabIndex + 1) % tabCount;
    }
    
    1. possible solution, using template-variable and pass through our click-function
    <button mat-raised-button (click)="demo2BtnClick(demo2tab)">Tab Demo 2!</button>
    <mat-tab-group #demo2tab>
        <mat-tab label="Tab 1">Content 1</mat-tab>
        <mat-tab label="Tab 2">Content 2</mat-tab>
    </mat-tab-group>
    
    public demo2BtnClick(tabGroup: MatTabGroup) {
      if (!tabGroup || !(tabGroup instanceof MatTabGroup)) return;
    
      const tabCount = tabGroup._tabs.length;
      tabGroup.selectedIndex = (tabGroup.selectedIndex + 1) % tabCount;
    }
    
    1. possible solution, using @ViewChild
    <button mat-raised-button (click)="demo3BtnClick()">Tab Demo 3!</button>
    <mat-tab-group #demo3Tab>
        <mat-tab label="Tab 1">Content 1</mat-tab>
        <mat-tab label="Tab 2">Content 2</mat-tab>
    </mat-tab-group>
    
    @ViewChild("demo3Tab", { static: false }) demo3Tab: MatTabGroup;
    
    public demo3BtnClick() {
      const tabGroup = this.demo3Tab;
      if (!tabGroup || !(tabGroup instanceof MatTabGroup)) return;
    
      const tabCount = tabGroup._tabs.length;
      tabGroup.selectedIndex = (tabGroup.selectedIndex + 1) % tabCount;
    }
    

    live-demo: https://stackblitz.com/edit/angular-selecting-mattab?file=src%2Fapp%2Fapp.component.ts

    0 讨论(0)
  • 2020-12-14 00:40

    In case it helps anyone, it is also possible to set selectedIndex on the MatTabGroup in your component.

    If your HTML has: <mat-tab-group #tabs>, you can get a reference to it in the component using @ViewChild('tabs') tabGroup: MatTabGroup;.

    Then you can do this.tabGroup.selectedIndex = newIndex; in the OnInit function, or elsewhere.

    0 讨论(0)
提交回复
热议问题