Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Material Select open with button

I have a select dropdown that I want to open with a button click, there is no mention of this anywhere in the docs - I have tried using an element reference and using select.open() on the element but it doesn't work. Has anyone else run into this issue?

<button ngClass="menu-filter-item-button" type="button" mat-button (click)="select.open()">
    <strong class="menu-filter-item-title">{{filter.name}}</strong>
    <mat-icon>keyboard_arrow_down</mat-icon>
</button>
<mat-select #select multiple (change)="onSubmit($event)" [compareWith]="compareById" [(ngModel)]="filter.value">
    <mat-option *ngFor="let value of filter.default" [value]="value">
        {{value}}
    </mat-option>
</mat-select>
like image 833
Jeff Voss Avatar asked Dec 21 '17 22:12

Jeff Voss


1 Answers

I had the same problem and this dirty hack solved it without displaying the mat-select itself:

HTML:

<button
  mat-icon-button
  matTooltip="Language"
  (click)=select.open()>

  <mat-icon>language</mat-icon>

  <mat-select
    #select
    [(ngModel)]="setLang"
    class="langSelect">
    <mat-option (click)="changeLang()" value="en">English</mat-option>
    <mat-option (click)="changeLang()" value="de">Deutsch</mat-option>
  </mat-select>

</button>

CSS:

::ng-deep .langSelect div.mat-select-arrow-wrapper {
  display: none;
}

::ng-deep .langSelect.mat-select {
  display: inline;
}

In my project it looks better than on StackBlitz, but anyway here is a link to this code on StackBlitz.

If you want to to simply have an additional button that opens the mat-select too this will work for you (no css needed):

<button mat-icon-button matTooltip="Language" (click)=select.open()>
  <mat-icon>language</mat-icon>
  <mat-select #select [(ngModel)]="setLang">
    <mat-option (click)="changeLang()" value="en">English</mat-option>
    <mat-option (click)="changeLang()" value="de">Deutsch</mat-option>
  </mat-select>
</button>
like image 65
Night Train Avatar answered Oct 09 '22 15:10

Night Train