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

后端 未结 14 1751
北恋
北恋 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: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 :

        
            
                {{category.name}}
            
        
    

    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.

提交回复
热议问题