components icon indicating copy to clipboard operation
components copied to clipboard

mat-select - need Check All & Uncheck All options (Select All & Deselect All) for multiple selection

Open jayakrishna-v opened this issue 7 years ago • 34 comments
trafficstars

Bug, feature request, or proposal:

Request: Need options for Check All and Uncheck All for multiple selection.

What is the expected behavior?

We can have 2 options at the top of the options panel to Check all or Uncheck all options in a single click. Something like this https://silviomoreto.github.io/bootstrap-select/examples/#selectdeselect-all-options

What is the current behavior?

Currently user has to click on each and every option in order to check or uncheck all the options available in the select control.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 5

jayakrishna-v avatar Dec 28 '17 12:12 jayakrishna-v

This use case is very common. I was quite surprise that angular material 2 does not support this....

Anyway hoping to see this feature soon :)

weijyewang avatar Feb 06 '18 10:02 weijyewang

I suppose that one checkbox "Select all" on the top would be more intuitive. And if items selected partially, then checkbox will be in indeterminate state

atamansv avatar Feb 06 '18 11:02 atamansv

Here is a solution for multi select select all that I wrote, feel free to use it. https://stackblitz.com/edit/angular-material-select-multi-c6vtux

ag-susini avatar Feb 22 '18 10:02 ag-susini

Another option is to put a mat-checkbox inside of your mat-select and use the indeterminate and checked properties. It might look something like this and live inside of your mat-select. There is some context missing, but it's the general idea.

<mat-checkbox class="mat-option"
                    (click)="$event.stopPropagation()"
                    (change)="selectAll(/* Check to see if you want to check/uncheck all, do that here */)"
                    [indeterminate]="itemsSelected.length && itemsNotSelected.length"
                    [checked]="!itemsNotSelected.length">
        Select All
      </mat-checkbox>

vesrah avatar Apr 03 '18 17:04 vesrah

Hi all!

I came out with a very simple solution, just add disabled property to your options to react when the all options were selected.

Unfortunally the mat-option component doesn't expose the selected property for input :(

<mat-form-field>
<mat-select name="stores" placeholder="Stores" [(ngModel)]="form.stores" multiple required>
  <mat-option #allS  [value]="-1">All</mat-option>
  <mat-option [disabled]="allS.selected" *ngFor="let s of stores | async" [value]="s.id">
    {{ s.name }}
  </mat-option>
</mat-select>
</mat-form-field>

Hope it helps

paulogr avatar Apr 10 '18 15:04 paulogr

I use reactive forms, and also needed to implement this. I ended up using modified version of what @HashSix put out there (thanks!)

If anyone comes along, here is my version using reactive form:
https://stackblitz.com/edit/angular-material-select-multi-c3agwh

In my project i just use checkbox outside of the select itself, and trigger select/deselect from that.

kmkatsma avatar Jul 27 '18 01:07 kmkatsma

Would be great to have this as a standard part of angular material.

For now, inspired by the solutions from both @HashSix and @vesrah, I've created a simple reusable component that can be added before the the first : https://stackblitz.com/edit/angular-material-select-all

RobertDiPaolo avatar Aug 03 '18 14:08 RobertDiPaolo

Note this needs to work for groups - for now, I've personally just implemented this something like:

<mat-select placeholder="Stuff"
            [(value)]="selectedStuff"
            multiple>
    <mat-optgroup *ngFor="let group of this.optionGroups"
                    [label]="group.header">
        <mat-option class="select-all" disabled>
            <button mat-button
                    (click)="selectAll(group.children)">
                Select All
            </button>
            <button mat-button
                    (click)="selectAll(group.children, false)">
                Deselect All
            </button>
        </mat-option>
        <mat-option *ngFor="let item of group.children"
                    [value]="item">
            {{item}}
        </mat-option>
    </mat-optgroup>
</mat-select>

Noting in selectedStuff we need to filter out the empty select all options.
What would be nice is if someone could tell me how to style these as simple links & move them up beside the group header?

nite avatar Aug 03 '18 15:08 nite

I did this:

    <span>
        <mat-form-field class="search-max-width" color="warn">
          <mat-select placeholder="Segments" [formControl]="segments" multiple
                      (selectionChange)="toggleSegments()">
            <mat-option (onSelectionChange)="toggleAllSegments($event)" value="all">All</mat-option>
            <mat-option *ngFor="let segment of allSegments" [value]="segment">
              {{segment}}
            </mat-option>
          </mat-select>
        </mat-form-field>
    </span>

in ts => segments = new FormControl([]); and set values conditionally.. But using angular 6 ;)

adornala avatar Sep 29 '18 01:09 adornala

<mat-form-field class="select-form">
    <mat-select 
    placeholder="Years" 
    name="year" 
    class="filter-select" 
    [(ngModel)]="selectedYears" 
    multiple 
    #yearSelect="ngModel">
        <mat-option 
          (click)="selectAll(yearSelect, years)" [value]="years[0]">
          Seleccionar todos
        </mat-option>
      <mat-option *ngFor="let year of years| slice:1; let i = index;" [value]="year">
        <span *ngIf="year.id > 0">
          {{year.viewValue}}
        </span>
      </mat-option>
    </mat-select>
</mat-form-field>

selectAll(select: NgModel, values) { if (Object.keys(this.selectedYears).length > 1) { this.checkAll = false; select.update.emit([]); } else { this.checkAll = true; select.update.emit(values); } }

This work for me with a checkbox. Is a extension of @HashSix answer.

vimrookie avatar Nov 30 '18 23:11 vimrookie

I would also like them to add this feature.

tinyweasel avatar Dec 10 '18 10:12 tinyweasel

+1 from me!

kingrollo avatar Feb 07 '19 01:02 kingrollo

+1 Would love to see this as a configurable option.

renaldas-kerpe-arria avatar Feb 18 '19 10:02 renaldas-kerpe-arria

  • 1 for mat-selection-list as well

jsonod avatar Feb 22 '19 16:02 jsonod

I would like this aswell. In the meantime I added a select-all component that can be used. It is similar to the one by @RobertDiPaolo but it doesn't take the mat-select options and value as inputs. https://stackblitz.com/edit/angular-material-abra-select-all

Abrissirba avatar Mar 19 '19 16:03 Abrissirba

@jsonod

  • 1 for mat-selection-list as well

This is already/now a feature: https://material.angular.io/components/list/api#MatSelectionList See selectAll() and deselectAll() methods.

What would be nice is if these two methods also triggered the selectionChange event.

danww avatar Apr 03 '19 02:04 danww

Indeed, to be consistent with mat-selection-list.

Currently I am doing: this.usersSelect.options.forEach(k => k.select());

working fine, however, less use of ViewChild in the ts code will be better for application programming.

zijianhuang avatar Jul 25 '19 23:07 zijianhuang

allS.selected

with disabled property we can't uncheck selected mat-option in mat-selected

AjitHKumarMarimuthu avatar Sep 30 '19 08:09 AjitHKumarMarimuthu

that is weird that so much time after, such an important feature is not yet provided.

demiro avatar Oct 30 '20 08:10 demiro

Still no update on this matter?

vbozhinovski avatar Jun 22 '21 11:06 vbozhinovski

Any updates on such an important feature request?

kim-spotinst avatar Aug 09 '21 10:08 kim-spotinst

this is still needed

bionara avatar Oct 08 '21 11:10 bionara

can it please get an higher priority? 🧐 @jelbourn

kim-spotinst avatar Oct 14 '21 15:10 kim-spotinst

This is needed. Any updates yet?

amirchappalwala avatar Oct 21 '21 10:10 amirchappalwala

4 Years now since the initial request :). Happy 2022 - still no updated yet?

Magnamura avatar Jan 07 '22 10:01 Magnamura

Hoping this is somewhere in the works? Has anyone found an implementation outside of angular material for multi-select with select all (and also preferably a search filter)?

Huiet avatar Apr 01 '22 16:04 Huiet

any update on this feature ?

sridhar-natuva avatar Oct 18 '22 14:10 sridhar-natuva

Angular material worst library in terms of functionality. You cant use this library in enterprise application.

taqishah1214 avatar Nov 06 '22 12:11 taqishah1214

Welcome to angular, prehistoric framework.

IbanezDenis28 avatar Feb 03 '23 20:02 IbanezDenis28

The problem with all the workarounds presented so far is that none of them are accessible. I have the feature built out and working, however I realized the 'Select All' and 'Clear All' buttons can't be navigated to via keyboard and came here looking for solutions. The only way you can arrow-up or arrow-down between elements in a mat-select dropdown is if they are components. Tabbing or Shift + Tabbing closes the dropdown and moves you to the next / previous DOM element.

Jayboy75 avatar Aug 09 '23 20:08 Jayboy75