如何将切换按钮嵌入到 mat-options 中?

huangapple go评论75阅读模式
英文:

How to embed the toggle button into the mat-options?

问题

I need to embed the toggle button or radio button inside the mat-options list. The desired design is as follows.

问题:

我已经使用以下代码将切换按钮添加到选项列表中,但是点击切换按钮会选中或取消选项,而不是更改切换状态。

<mat-option (click)="handleSelected(item)" *ngFor="let item of defaultSelectValue | dataSearch : field.searchCtrl.value" [value]="field.keySelect ? item[this.field.selectKey] : item">
    {{ item[this.field.selectValue] }} 
    <mat-slide-toggle></mat-slide-toggle>
</mat-option>

请指导我如何以使两者相应工作的方式来实现它?

英文:

I need to embed the toggle button or radio button inside the mat-options list. The desired design is as follows.

如何将切换按钮嵌入到 mat-options 中?

ISSUE:

I have added the toggle button inside the options list with the following code but by clicking on the toggle button the option is checked or unchecked rather than changing the toggle state.

&lt;mat-option (click)=&quot;handleSelected(item)&quot; *ngFor=&quot;let item of defaultSelectValue | dataSearch : field.searchCtrl.value&quot; [value]=&quot;field.keySelect ? item[this.field.selectKey] : item&quot;&gt;
    {{ item[this.field.selectValue] }} 
&lt;mat-slide-toggle&gt;&lt;/mat-slide-toggle&gt;
&lt;/mat-option&gt;

Please guide me that, How can I achieve it in a way that both things work correspondingly?

答案1

得分: 0

以下是翻译好的部分:

我们可以通过事件传播的帮助来实现这一点,如下面的代码片段所示。

<mat-form-field>
  <mat-label>选择选项</mat-label>
  <mat-select [(ngModel)]="selectedOptions" multiple>
    <mat-option *ngFor="let option of options" [value]="option.value">
      <span>{{ option.label }}</span>
      <mat-slide-toggle (click)="$event.stopPropagation()"></mat-slide-toggle>
    </mat-option>
  </mat-select>
</mat-form-field>
英文:

We can achieve this with the help of event propagation as shown in below code snippet.

&lt;mat-form-field&gt;
  &lt;mat-label&gt;Select options&lt;/mat-label&gt;
  &lt;mat-select [(ngModel)]=&quot;selectedOptions&quot; multiple&gt;
    &lt;mat-option *ngFor=&quot;let option of options&quot; [value]=&quot;option.value&quot;&gt;
      &lt;span&gt;{{ option.label }}&lt;/span&gt;
      &lt;mat-slide-toggle (click)=&quot;$event.stopPropagation()&quot;&gt;&lt;/mat-slide-toggle&gt;
    &lt;/mat-option&gt;
  &lt;/mat-select&gt;
&lt;/mat-form-field&gt;

huangapple
  • 本文由 发表于 2023年5月30日 06:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定