Material Angular Multi-Select drop down With Select All Check Box
Material Angular drop down as multi select with select all option using mat check box.
- Refer material angular basics @ Material Angular Io.
- Refer How to Start Material Angular @ InterfaceCreator.
- Refer How to Start Angular 6 step by step @ InterfaceCreator.
Material Angular Modules Used :
- import {MatCheckboxModule} from '@angular/material/checkbox'; refer
- import {MatSelectModule} from '@angular/material/select';refer
- import {MatFormFieldModule} from '@angular/material/form-field'; refer
Step 1 : Import MatCheckboxModule,MatSelectModule and MatFormFieldModule from Material Angular on app.module.ts
Step 2: in required component.ts file add variables selectall as boolean, languages as formcontrol type variable and an array of data.
selectall: boolean;Step 3: Add a function selectalllang() required component.ts.
languages = new FormControl();
languagesList: string[] = ['English', 'Spanish', 'Russian', 'Arabic','Mandarin Chinese','Malay','Bengali'];
selectalllang() {Step 4: In Components HTML Template Add mat-checkbox, Assing ngModel to selectall and handle the ngModelChange event with selectalllang
console.log('call',[this.selectall,this.languages]);
if (this.selectall === false) {
this.languages = new FormControl();
return;
}else if (this.selectall === true) {
this.languages = new FormControl();
this.languages.setValue(this.languagesList);
}
}
<mat-checkbox [(ngModel)]="selectall" (ngModelChange)="selectalllang()" >All</mat-checkbox>Step 5: In Components HTML Template add mat-select , assign formcontrol to languages and mention multiple directives to enable multi-selection
<mat-form-field>Step 6: Run the project and See the output.
<mat-select placeholder="Languagess" [formControl]="languages" multiple>
<mat-option *ngFor="let language of languagesList" [value]="language">{{language}}</mat-option>
</mat-select>
</mat-form-field>
Code Action @ StackBlitz
0 Comments