Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mat-select with checkboxes

I really want the mat-select basic functionality but with checkboxes. If I turn on [multiple] I get the checkboxes but I also get multi-select functionality, I need single select and checkbox.

like image 818
JBoothUA Avatar asked Aug 28 '18 14:08

JBoothUA


People also ask

What is a mat select trigger?

MatSelectTrigger. Allows the user to customize the trigger that is displayed when the select has a value.

How do you set mat selected value?

Create Select using <mat-select> Angular Material Select is created using <mat-select> which is a form control for selecting a value from a set of options. To add elements to select option, we need to use <mat-option> element. To bind value with <mat-option> , we need to use value property of it.

How do you check if a checkbox is checked or not in angular?

The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.


1 Answers

you can achieve your requirements like this,

here's is example,

in app.component.html

<mat-form-field>
    <mat-select placeholder="Toppings">
        <mat-option *ngFor="let topping of toppingList; let i =index" [value]="topping">
            <mat-checkbox [checked]="selected === i" (change)="onChange(topping);selected = i"> {{topping}}</mat-checkbox>
        </mat-option>
    </mat-select>
</mat-form-field>

and in app.component.ts

export class AppComponent {
  toppingList: string[] = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage'];
  selected = -1;

  /*checkbox change event*/
  onChange(event) {
    console.log(event)
  }
}

here's is Stackblitz demo

like image 67
Aniket Avhad Avatar answered Oct 06 '22 03:10

Aniket Avhad