The problem is you have a single variable allSelected
to control both of the mat-select
boxes, and you have also given both mat-select
boxes the same ID of mySel
. This is why it selects all on the first mat-select
when you use the select all option in the second one.
To fix this, give each mat-select
a different ID e.g mySelBranch
and mySelCategory
:
<mat-form-field appearance="fill">
<mat-label>Branch</mat-label>
<mat-select #mySelBranch [formControl]="branch" multiple>
<mat-option [value]="0" (click)="toggleAllSelection(mySel)">All items</mat-option>
<mat-option *ngFor="let branch of branchList" [value]="branch">{{branch}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Category</mat-label>
<mat-select #mySelCategory [formControl]="categoryDescriptions" multiple>
<mat-option [value]="0" (click)="toggleAllSelection(mySel2)">All items</mat-option>
<mat-option *ngFor="let category of categoryList" [value]="category">{{category}}</mat-option>
</mat-select>
</mat-form-field>
And you'll notice I've also changed the call to toggleAllSelection
- it now passes the mat-select in as a parameter.
The toggleAllSelection
has been changed to:
toggleAllSelection(matSelect: MatSelect) {
const isSelected: boolean = matSelect.options
// The "Select All" item has the value 0, so find that one
.filter((item: MatOption) => item.value === 0)
// Get the value of the property 'selected' (this tells us whether "Select All" is selected or not)
.map((item: MatOption) => item.selected)
// Get the first element (there should only be 1 option with the value 0 in the select)
[0];
if (isSelected) {
matSelect.options.forEach((item: MatOption) => item.select());
} else {
matSelect.options.forEach((item: MatOption) => item.deselect());
}
}
The first part of the function looks through the options of the mat-select
passed in as a paramter, and finds the option with the value 0
(due to how you've defined your HTML, the "Select All" option always has the value 0
). It then gets the value of the property name selected
(this will be true
is "Select All" is selected and false if it is deselected). As the result of this is an array, it then uses [0]
to get the first item (there should only be a single item in the array at this point because of the filter
function used:
const isSelected: boolean = matSelect.options
.filter((item: MatOption) => item.value === 0)
.map((item: MatOption) => item.selected)[0];
You no longer need the following variables in your TypeScript class:
@ViewChild("mySel") skillSel: MatSelect;
allSelected = false;
Please see this StackBlitz for a demo.