Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Mat Select Multiple selectionchange finding which option was changed

I have a <mat select> with the multiple option set (a multi-select). When the selectionChange event fires I need to know which option has been checked or unchecked, however, it only returns the new current list of selected options.

For instance I have a list:

<mat-select (selectionChange)="change($event)" multiple placeholder="Select">   <mat-option value="1">one</mat-option>   <mat-option value="2">two</mat-option>   <mat-option value="3">three</mat-option>   <mat-option value="4">four</mat-option> </mat-select> 

If options one, three and four are checked and then the user unchecked option four, in the event handler I need to know which option triggered the event (i.e. option four) and its new state. I currently don't see a way of accessing that information in the selectionChange event.

https://stackblitz.com/edit/angular-1e9gsd?file=app/select-overview-example.ts

I tried putting the event handler (selectionChange)="change($event)" on the <mat-option> but it doesn't seem to be supported.

like image 641
Curtis Avatar asked May 31 '18 00:05

Curtis


People also ask

How do I use selectionChange in mat-select?

The event selectionChange is used with <mat-select> element as following. Our function onBookChange() will execute every time when there is selection change. To get the selected value we can pass $event to the function. We can also get selected value using FormControl , FormGroup and NgModel .

How do you disable mat-select option once it has been selected?

It is possible to disable the entire select or individual options in the select by using the disabled property on the <select> or <mat-select> and the <option> or <mat-option> elements respectively.

What is compareWith in mat-select?

The compareWith just literally compares objects by some given values and the checkboxes get selected, if it returns true . In my code, I decided to go with the ng-Model binding but it works with formControl as well: <mat-form-field> <mat-select multiple[compareWith]="compareFn" name="users. [(ngModel)]="users">

How do you get the selected value of mat-select?

To add elements to Select option, we need to use <mat-option> element and to bind value with <mat-option> , use value property of it. To set and get a value for <mat-select> , use value , ngModel , formControl and formControlName property.


2 Answers

I needed to use onSelectionChange on the <mat-option>, which is different than the selectionChange that you can use on the <mat-select>

It would be nice if that was in the documentation for mat-select.

Here it is working https://stackblitz.com/edit/angular-1e9gsd-34hrwg?file=app/select-overview-example.html

like image 179
Curtis Avatar answered Sep 18 '22 13:09

Curtis


Here is alternative solution. onSelectionChange event is fired before [(ngModel)] is binded. In my case, I needed both binded data and last selected value.

<mat-select multiple [(ngModel)]="selectedValues" name="hebele" #select> <mat-option #matOption *ngFor="let value of data.Values" (click)="yourMethod(matOption)" [value]="value">         {{value.Text}} </mat-option> </mat-select>   yourMethod(data: any) {    let lastSelectedValue = data.value;    const isChecked = data.selected;  } 
like image 20
M.Erkan Çam Avatar answered Sep 19 '22 13:09

M.Erkan Çam