Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select All mat option and deselect All

I have scenario as below:

enter image description here

I want to achieve is:

  1. When user click on All then all options shall be selected and when user click All again then all options shall be deselcted.
  2. If All option is checked and user click any other checkbox than All then All and clicked checkbox shall be deselected.
  3. When user selects 4 options one by one then All shall be selected.

HTML file

<mat-select placeholder="User Type" formControlName="UserType" multiple>     <mat-option *ngFor="let filters of userTypeFilters" [value]="filters.key">           {{filters.value}}     </mat-option>         <mat-option #allSelected (click)="toggleAllSelection()" [value]="0">All</mat-option> </mat-select> 

TS file

this.searchUserForm = this.fb.group({   userType: new FormControl('') });  userTypeFilters = [   {     key: 1, value: 'Value 1',   },   {     key: 2, value: 'Value 2',   },   {     key: 3, value: 'Value 3',   },   {     key: 4, value: 'Value 4',   } ]  toggleAllSelection() {   if (this.allSelected.selected) {     this.searchUserForm.controls.userType     .patchValue([...this.userTypeFilters.map(item => item.key), 0]);   } else {     this.searchUserForm.controls.userType.patchValue([]);   } } 

Now, how to achieve 2nd and 3rd point

Stackblitz is: https://stackblitz.com/edit/angular-material-with-angular-v5-znfehg?file=app/app.component.html

like image 518
Gags Avatar asked Jul 29 '18 11:07

Gags


People also ask

Can we use NgModel in mat-select?

We can use NgModel to get and set values in Select option for Angular Material Select as well as native Select.

Why does allselected select all on the first mat-select box?

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.

How does toggleallselection work with Mat-select?

And you'll notice I've also changed the call to toggleAllSelection - it now passes the mat-select in as a parameter. 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 ).

How to deselect all options in a checkbox?

When user click on All then all options shall be selected and when user click All again then all options shall be deselcted. If All option is checked and user click any other checkbox than All then All and clicked checkbox shall be deselected.

How to select or unselect all selected elements on every click?

Another way to do this is with the @ViewChild selector to get the mat-select component and troggle the mat-options items selected or unselected. We need also a variable to save the selected actual status to select or unselect all the elements on every click. Hope will help.


1 Answers

Use code as below create function on click each mat-option and select()/deselect() all option:

See stackblitz:https://stackblitz.com/edit/angular-material-with-angular-v5-jsgvx6?file=app/app.component.html

TS:

togglePerOne(all){     if (this.allSelected.selected) {       this.allSelected.deselect();     return false; }   if(this.searchUserForm.controls.userType.value.length==this.userTypeFilters.length)     this.allSelected.select();  }   toggleAllSelection() {     if (this.allSelected.selected) {       this.searchUserForm.controls.userType         .patchValue([...this.userTypeFilters.map(item => item.key), 0]);     } else {       this.searchUserForm.controls.userType.patchValue([]);     }   } 

HTML:

<form [formGroup]="searchUserForm" fxFlex fxLayout="column" autocomplete="off" style="margin: 30px">     <mat-select placeholder="User Type" formControlName="userType" multiple>         <mat-option *ngFor="let filters of userTypeFilters" [value]="filters.key" (click)="togglePerOne(allSelected.viewValue)">             {{filters.value}}         </mat-option>         <mat-option #allSelected (click)="toggleAllSelection()" [value]="0">All</mat-option>     </mat-select> </form> 
like image 51
לבני מלכה Avatar answered Sep 28 '22 01:09

לבני מלכה