首页 > 解决方案 > 如何防止更改 MatTab 直到确认?

问题描述

我想保留,更改MatTab直到得到确认。我已用于MatDialog确认。问题是,在单击“是”之前,该选项卡已经更改。

例如,从收入选项卡,我单击调整选项卡。在切换到该选项卡之前,我需要先显示弹出窗口但是在移动到调整选项卡后我得到了弹出窗口。

显示弹出窗口的图像

组件模板:

 <mat-tab-group (click)="tabClick($event)">
  <mat-tab *ngFor="let tab of tabs; let index = index" [label]="tab">
    <app-spread></app-spread
  </mat-tab>
</mat-tab-group>

组件 ts(onClick 的方法):

tabClick(clickEvent: any) {
    if (clickEvent.target.innerText != 'First') {
      this.confirm();
    }
  }
  public async confirm() {
    const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
      maxHeight: '200px',
      maxWidth: '767px',
      width: '360px',
      disableClose: true,
      data: {
        title: 'Confirmation Message',
        content:
          'There are valid statements that are not Final. Set the statements as Final?'
      }
    });
    const res = dialogRef.afterClosed().subscribe(result => {
      if (result === 1) {
        //TODO need to change the tab
      } else {
        //TODO no need to change the tab
      }
    });
  }

标签: angulartypescriptangular-materialmat-tab

解决方案


前段时间我在这个SO中模拟了一个“mat-tab”

我想象你可以使用它只改变功能

<mat-tab-group #tabgroup style="margin-bottom:5px;" 
               animationDuration="0"   mat-align-tabs="start"
               (selectedIndexChange)="change(tabgroup,$event)">
...
</mat-tab-group>

功能变化:

  change(tab:any,index:number)
  {
    if (tab.selectedIndex!=this.indexOld){
      const dialogRef = this.dialog.open(....)
      const res = dialogRef.afterClosed().subscribe(result => {
        if (result === 1) {
           this.index=index;
        } else {
           tab.selectedIndex=this.indexOld;
        }
      });
    }
    else
    {
       this.index=index;
    }
  }

查看stackblitz - 在stackblitz中我简单地使用确认对话框-


推荐阅读