简体   繁体   English

mat-select selectionChange 获取组角度

[英]mat-select selectionChange get group angular

I have control mat-select with two mat-optgroup groups which has an event handler (selectionChange)="applicationSelected($event.value, i) .我有两个mat-optgroup组的控制mat-select ,它们有一个事件处理程序(selectionChange)="applicationSelected($event.value, i)

How can I detect from which group an option was selected?如何检测从哪个组中选择了选项?

There isn't an easy, direct way to know the group from the selectionChange event.没有一种简单、直接的方法可以从selectionChange事件中了解组。 It only tells you the source (MatSelect) and the selected value.它只告诉您来源 (MatSelect) 和选定的值。 But the onSelectionChange event of MatOption gives you access to the MatOption which in turn gives access to the MatOptionGroup.但是 MatOption 的onSelectionChange事件使您可以访问 MatOption,而后者又可以访问 MatOptionGroup。 For example:例如:

<mat-option (onSelectionChange)="optionSelected($event)" ...>...</mat-option>

optionSelected(event: MatOptionSelectionChange) {
    console.log(event.source.group.label);
}

I ran into this issue as well, but keep in mind, the expected behavior of onSelectionChanged() is the following: A selection change event is fired not only when an option is selected but also when it is deselected .我也遇到了这个问题,但请记住, onSelectionChanged() 的预期行为如下: 选择更改事件不仅在选择选项时触发,而且在取消选择时触发。 So when an option is selected, two events are fired: the users selection and the option that is now deselected.因此,选择选项后,将触发两个事件:用户选择和现在取消选择的选项。 As such, you'll see 2 events being fired.因此,您将看到 2 个事件被触发。 The first of which is the desired one.其中第一个是所需的。 You can see if event.isUserInput is set to true (desired event) and also send in the group.name to the event handler:您可以查看 event.isUserInput 是否设置为 true(所需事件)并将 group.name 发送到事件处理程序:

In your component html:在您的组件 html 中:

 <mat-select formControlName="category" required> <mat-optgroup *ngFor="let group of categoryGroups" [label]="group.name"> <mat-option *ngFor="let category of group.options" [value]="category.value" (onSelectionChange)="onCategorySelection($event, group.name)"> {{category.viewValue}} </mat-option> </mat-optgroup> </mat-select>

And the function you bind to the event in the component class:以及您绑定到组件类中的事件的函数:

 onCategorySelection(event: MatOptionSelectionChange, groupName: string) { if (event.isUserInput) { this.groupName = groupName; // event.source.group.label; } }

I also think it's little funny that the mat-feature doesn't give an easy access to the group values, because it's all the idea of group to separate the select options to some groups and in many cases the use case is to wrap at the logic level the group data with the options selected.我还认为 mat-feature 不能轻松访问组值,这有点有趣,因为 group 的所有想法都是将选择选项分离到某些组,并且在许多情况下,用例是在使用所选选项对组数据进行逻辑级别处理。

That's my solution:这是我的解决方案:

<mat-form-field >
  <mat-label>Filter By</mat-label>
  <mat-select  panelClass="" #choosedValue (valueChange)="doSomething1(choosedValue.value)" >
    <mat-option >-- None --</mat-option>
    <mat-optgroup  #group1 *ngFor="let group of filterData" [label]="group.viewValue"
                   style = "background-color: #0c5460">
      <mat-option #mmm *ngFor="let option of group.options" [value]="{value: option.value, groupValue: group.value}" >
        {{option.viewValue}}
      </mat-option>
    </mat-optgroup>
  </mat-select>
</mat-form-field>

and the function :和功能:

doSomething1(value: any){
    console.log("do something with ", value);//result: {value: "Honda", groupValue: "cars"}
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM