如何编程更改mat-button-toggle组状态

发布于 2025-02-13 02:39:56 字数 1409 浏览 0 评论 0原文

我正在尝试使用确认对话框更改MatbuttontoogleGroup状态,但是我找不到一种防止默认行为的方法(仅当我在CANCAL上更改为先前的状态时)。

html文件:

<mat-button-toggle-group [value]="value" (change)="onChange($event)">
  <mat-button-toggle [value]="StatusType.Open">
    Open
  </mat-button-toggle>
  <mat-button-toggle [value]="StatusType.Closed">
    Closed
  </mat-button-toggle>
</mat-button-toggle-group>

ts file:

@Input() value: StatusType;
@Output() statusChange = new EventEmitter<StatusType>();

onChange(event: MatButtonToggleChange): void {
   const newValue = event.value;

    if (newValue === StatusType.Open) {
      this.statusChange.emit(value);

      return;
    }

    const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
      data: {
        title: 'Confirm status change',
        message: 'Are you sure?',
      }
    });

    dialogRef
      .afterClosed()
      .pipe(
        untilDestroyed(this),
        filter((result) => !!result)
      )
      .subscribe(() => {
        this.statusChange.emit(newValue);
      });
}

我实际需要的是在onChange函数的开头添加event.preventDefault(),但是matbuttontoggglechange 事件没有destrestdefault()方法,因为在幕后它实现了controlValueAccessor

I'm trying to change the MatButtonToogleGroup state with a confirmation dialog, but I can't find a way to prevent the default behavior (only if I change back to the previous state on Cancel).

HTML file:

<mat-button-toggle-group [value]="value" (change)="onChange($event)">
  <mat-button-toggle [value]="StatusType.Open">
    Open
  </mat-button-toggle>
  <mat-button-toggle [value]="StatusType.Closed">
    Closed
  </mat-button-toggle>
</mat-button-toggle-group>

TS file:

@Input() value: StatusType;
@Output() statusChange = new EventEmitter<StatusType>();

onChange(event: MatButtonToggleChange): void {
   const newValue = event.value;

    if (newValue === StatusType.Open) {
      this.statusChange.emit(value);

      return;
    }

    const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
      data: {
        title: 'Confirm status change',
        message: 'Are you sure?',
      }
    });

    dialogRef
      .afterClosed()
      .pipe(
        untilDestroyed(this),
        filter((result) => !!result)
      )
      .subscribe(() => {
        this.statusChange.emit(newValue);
      });
}

And what I actually need is to add an event.preventDefault() at the beginning of the onChange function, but the MatButtonToggleChange event does not have the preventDefault() method because behind the scenes it implements the ControlValueAccessor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猥︴琐丶欲为 2025-02-20 02:39:56

您可以尝试做的事情是使用ngmodel。您可以在组或所有切换按钮上添加ClickListener,然后打开对话框。用户确认对话框后,您可以设置模型。

html文件

<mat-button-toggle-group [(ngModel)]="value" (change)="onChange($event)">
  <mat-button-toggle [value]="StatusType.Open" (click)="onClick($event)">
    Open
  </mat-button-toggle>
  <mat-button-toggle [value]="StatusType.Closed" (click)="onClick($event)">
    Closed
  </mat-button-toggle>
</mat-button-toggle-group>

ts文件

  onClick(event: any): void {
    const newValue = event.value;

    if (newValue === StatusType.Open) {
      this.statusChange.emit(value);

      return;
    }

    const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
      data: {
        title: 'Confirm status change',
        message: 'Are you sure?',
      }
    });

    dialogRef
      .afterClosed()
      .pipe(
        untilDestroyed(this),
        filter((result) => !!result)
      )
      .subscribe(() => {
        this.statusChange.emit(newValue);
      });
  }

这是一个stackblitz,其中一个示例很少:
https://stackblitz.com/edit/angular-layjkm?file=src%2fapp%2fbutton-toggle-appearance-example.ts

Something you could try to do is to use ngModel instead. You can add a clickListener on the group or all the toggle buttons, and then open the dialog. After the user confirms the dialog, you can set the model.

HTML File

<mat-button-toggle-group [(ngModel)]="value" (change)="onChange($event)">
  <mat-button-toggle [value]="StatusType.Open" (click)="onClick($event)">
    Open
  </mat-button-toggle>
  <mat-button-toggle [value]="StatusType.Closed" (click)="onClick($event)">
    Closed
  </mat-button-toggle>
</mat-button-toggle-group>

TS File

  onClick(event: any): void {
    const newValue = event.value;

    if (newValue === StatusType.Open) {
      this.statusChange.emit(value);

      return;
    }

    const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
      data: {
        title: 'Confirm status change',
        message: 'Are you sure?',
      }
    });

    dialogRef
      .afterClosed()
      .pipe(
        untilDestroyed(this),
        filter((result) => !!result)
      )
      .subscribe(() => {
        this.statusChange.emit(newValue);
      });
  }

Here's a Stackblitz with a minimal example:
https://stackblitz.com/edit/angular-layjkm?file=src%2Fapp%2Fbutton-toggle-appearance-example.ts

沫尐诺 2025-02-20 02:39:56

这将需要重新分配到类型 matbuttontogglechange 的内部状态。在组中需要使用 ngmodel 来设置默认选择。您可以浏览已发出的活动的所有属性并使用它。

这类骇客应被视为最后的度假胜地,而更好的UX解决方案或组件选择将解决问题的根本原因

<mat-button-toggle-group color="primary" (change)="changeItem($event)"
            [ngModel]="selectedItem">
            <mat-button-toggle [checked]="selectedItem.id === item.id" [value]="item" color="primary"
                *ngFor="let item of items">
                {{ item.label }}
            </mat-button-toggle>
        </mat-button-toggle-group>
changeItem(event: MatButtonToggleChange): void {
      const clickedOption = event.source.value;
      if (!confirm('Are you sure to change ?')) {
          event.source.buttonToggleGroup.value = this.selectedItem
      } else {
          this.selectedItem = event.source.buttonToggleGroup.value;
      }
}

这是Stackblitz示例 https://stackblitz.com/edit/angular-xjn6ga-yxwytq?file=main.ts 。与Angular 13这样的最新版本也可以正常工作。

This would need a reassignment to the internal state of the event of type MatButtonToggleChange emitted by the toggle component. Usage of ngModel is needed on the group to set a default selection. You can go through all the properties of the event emitted and use it.

These kind of hacks should be considered as the last resort and better UX solutions or component choice will solve the root cause of problem better

<mat-button-toggle-group color="primary" (change)="changeItem($event)"
            [ngModel]="selectedItem">
            <mat-button-toggle [checked]="selectedItem.id === item.id" [value]="item" color="primary"
                *ngFor="let item of items">
                {{ item.label }}
            </mat-button-toggle>
        </mat-button-toggle-group>
changeItem(event: MatButtonToggleChange): void {
      const clickedOption = event.source.value;
      if (!confirm('Are you sure to change ?')) {
          event.source.buttonToggleGroup.value = this.selectedItem
      } else {
          this.selectedItem = event.source.buttonToggleGroup.value;
      }
}

Here is the stackblitz example https://stackblitz.com/edit/angular-xjn6ga-yxwytq?file=main.ts. Works fine with recent versions like Angular 13 too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文