I have implemented a "Select All" feature in my select list to clear all other selected options and only choose the "All" option. However, I am facing an issue where I can clear out all selected options but cannot select the "All" option.
<mat-form-field class="example-full-width">
<mat-label>Select Program(s)</mat-label>
<mat-select #myprograms [formControl]="myprgrmFormControl" multiple [(ngModel)]="criteria.Program_ID" >
<mat-option (click)="toggleAllSelection()" value="ALL" >All</mat-option>
<mat-option *ngFor="let row of ProgramDropdown" [value]="row.Program_ID" >
{{row.Program_Name}} </mat-option>
</mat-select>
</mat-form-field>
In the typescript file:
import { MatSelect } from '@angular/material/select';
import { MatOption } from '@angular/material/core';
myprgrmFormControl = new FormControl();
allSelected = false;
@ViewChild('myprograms') ProgSel: MatSelect;
toggleAllSelection() {
this.allSelected = !this.allSelected;
if (this.allSelected) {
this.ProgSel.options.forEach( (item : MatOption) => item.deselect());
} else {
this.ProgSel.options.forEach( (item : MatOption) => {item.deselect()});
}
}
How can I select the "All" option programmatically in the typescript file?