Angular mat-selection-list, How to make single checkbox select similar to radio button?

后端 未结 14 1610
北恋
北恋 2021-02-18 21:22

How to make make single checkbox select from mat-selection-list. Similar to radio button which accepts one value from group of values.

相关标签:
14条回答
  • 2021-02-18 21:51

    Starting Angular Material 9.1.0, selection list supports a single selection mode. Enable it with the multiple input set to false: https://material.angular.io/components/list/api#MatSelectionList

    0 讨论(0)
  • 2021-02-18 21:56

    In order to make single checkbox select from mat-selection-list if you are not using Angular Material v6+ here is the workaround i used : Let's say we have a list of categories checkboxes.

    In your HTML :

        <mat-selection-list>
            <mat-list-option  (selectionChange)="handleSelection($event, category)" *ngFor="let category of categories" [value]="category">
                <span >{{category.name}}</span>
            </mat-list-option>
        </mat-selection-list>
    

    In your .TS :

      handleSelection(event, categorySelected) {
          if (event.selected) {
              event.source.selectionList.options.toArray().forEach(element => {
                  if (element.value.name!= categorySelected.name) {
                     element.selected = false;
                  }
             });
          }
      }
    

    The key here is to use the property selectionChange on the MatListOption and not MatSelectionList.

    0 讨论(0)
提交回复
热议问题