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

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

In Angular Material, mat-selection-list allows you to create a list of items with checkboxes for selection. If you want to make the selection behave like radio buttons (where only one item can be selected at a time), you can implement this behavior programmatically.

Approach

To achieve a single checkbox selection behavior similar to radio buttons:

  1. Track Selection State: Track the selected item in your component.
  2. Handle Selection Changes: Implement logic to update the selected item when a checkbox is clicked.

Example Implementation

Assume you have a list of items (items) and you want to implement single selection behavior:

HTML Template (component.html)

<mat-selection-list #list (selectionChange)="onSelectionChange($event)"> <mat-list-option *ngFor="let item of items" [value]="item" [selected]="item === selectedValue"> {{ item.name }} </mat-list-option> </mat-selection-list> 

Component Class (component.ts)

import { Component } from '@angular/core'; import { MatSelectionListChange } from '@angular/material/list'; @Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'] }) export class MyComponent { items = [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' } ]; selectedValue: any; // Track the selected item onSelectionChange(event: MatSelectionListChange) { // Ensure only one item is selected if (event.option.selected) { this.selectedValue = event.option.value; // Deselect other items this.items.forEach(item => { if (item !== event.option.value) { item.selected = false; } }); } else { this.selectedValue = null; } } } 

Explanation:

  • mat-selection-list: Angular Material component for displaying a list with selection options.
  • mat-list-option: Individual item within the list that acts as a selectable option.
  • [selected]="item === selectedValue": Sets the checkbox state based on whether the item matches the selectedValue.
  • onSelectionChange(event: MatSelectionListChange): Handler method called when selection changes.
  • Logic in onSelectionChange:
    • Updates selectedValue when an item is selected.
    • Ensures only one item (selectedValue) is selected at a time by deselecting others.

Considerations:

  • Styling: Customize styling to visually indicate single selection behavior (e.g., radio button appearance).
  • Accessibility: Ensure the UI is accessible and intuitive for users, especially when mimicking radio button behavior with checkboxes.

By implementing this approach, you can make a mat-selection-list behave like radio buttons where only one checkbox can be selected at a time. Adjust the example code based on your specific requirements and styling preferences.

Examples

  1. Angular mat-selection-list single checkbox behavior like radio button

    Description: Implementing a behavior where selecting a checkbox in mat-selection-list deselects others, mimicking radio button behavior.

    Code:

    <mat-selection-list #list> <mat-list-option checkboxPosition="before" (click)="list.options.forEach(o => o.selected = false); option.selected = true" #option> Option 1 </mat-list-option> <mat-list-option checkboxPosition="before" (click)="list.options.forEach(o => o.selected = false); option.selected = true" #option> Option 2 </mat-list-option> <mat-list-option checkboxPosition="before" (click)="list.options.forEach(o => o.selected = false); option.selected = true" #option> Option 3 </mat-list-option> </mat-selection-list> 
  2. Angular mat-selection-list single checkbox exclusive selection

    Description: Ensuring only one checkbox can be selected at a time in a mat-selection-list.

    Code:

    <mat-selection-list #list (selectionChange)="list.options.forEach(o => o.selected = o === $event.option)"> <mat-list-option checkboxPosition="before"> Option 1 </mat-list-option> <mat-list-option checkboxPosition="before"> Option 2 </mat-list-option> <mat-list-option checkboxPosition="before"> Option 3 </mat-list-option> </mat-selection-list> 
  3. Angular mat-selection-list single checkbox select/deselect

    Description: Implementing toggling behavior where clicking on a selected checkbox deselects it in a mat-selection-list.

    Code:

    <mat-selection-list #list (selectionChange)="list.options.forEach(o => o.selected = o === $event.option ? !o.selected : false)"> <mat-list-option checkboxPosition="before"> Option 1 </mat-list-option> <mat-list-option checkboxPosition="before"> Option 2 </mat-list-option> <mat-list-option checkboxPosition="before"> Option 3 </mat-list-option> </mat-selection-list> 
  4. Angular mat-selection-list single checkbox with radio button styling

    Description: Styling checkboxes in mat-selection-list to visually resemble radio buttons for exclusive selection.

    Code:

    <mat-selection-list #list> <mat-list-option checkboxPosition="before" (click)="list.options.forEach(o => o.selected = false); option.selected = true" #option> <mat-checkbox></mat-checkbox> Option 1 </mat-list-option> <mat-list-option checkboxPosition="before" (click)="list.options.forEach(o => o.selected = false); option.selected = true" #option> <mat-checkbox></mat-checkbox> Option 2 </mat-list-option> <mat-list-option checkboxPosition="before" (click)="list.options.forEach(o => o.selected = false); option.selected = true" #option> <mat-checkbox></mat-checkbox> Option 3 </mat-list-option> </mat-selection-list> 
  5. Angular mat-selection-list single checkbox with radio button selection

    Description: Handling selection logic in mat-selection-list to behave like radio buttons when clicking on checkboxes.

    Code:

    <mat-selection-list #list (selectionChange)="list.options.forEach(o => o.selected = o === $event.option ? true : false)"> <mat-list-option checkboxPosition="before"> <mat-checkbox></mat-checkbox> Option 1 </mat-list-option> <mat-list-option checkboxPosition="before"> <mat-checkbox></mat-checkbox> Option 2 </mat-list-option> <mat-list-option checkboxPosition="before"> <mat-checkbox></mat-checkbox> Option 3 </mat-list-option> </mat-selection-list> 
  6. Angular mat-selection-list single checkbox exclusive selection using ngModel

    Description: Using ngModel to manage the selection state of checkboxes in mat-selection-list, ensuring exclusive selection.

    Code:

    <mat-selection-list #list> <mat-list-option checkboxPosition="before" [(ngModel)]="selectedOption" (click)="selectedOption = option === selectedOption ? null : option"> Option 1 </mat-list-option> <mat-list-option checkboxPosition="before" [(ngModel)]="selectedOption" (click)="selectedOption = option === selectedOption ? null : option"> Option 2 </mat-list-option> <mat-list-option checkboxPosition="before" [(ngModel)]="selectedOption" (click)="selectedOption = option === selectedOption ? null : option"> Option 3 </mat-list-option> </mat-selection-list> 
  7. Angular mat-selection-list single checkbox with custom CSS for radio button effect

    Description: Applying custom CSS to checkboxes in mat-selection-list to achieve a radio button-like effect.

    Code:

    <mat-selection-list #list> <mat-list-option checkboxPosition="before" class="radio-like-checkbox" (click)="list.options.forEach(o => o.selected = false); option.selected = true" #option> Option 1 </mat-list-option> <mat-list-option checkboxPosition="before" class="radio-like-checkbox" (click)="list.options.forEach(o => o.selected = false); option.selected = true" #option> Option 2 </mat-list-option> <mat-list-option checkboxPosition="before" class="radio-like-checkbox" (click)="list.options.forEach(o => o.selected = false); option.selected = true" #option> Option 3 </mat-list-option> </mat-selection-list> <style> .radio-like-checkbox { display: flex; align-items: center; } .radio-like-checkbox mat-checkbox { margin-right: 8px; /* Adjust spacing between checkbox and text */ } </style> 
  8. Angular mat-selection-list single checkbox with custom directive for radio button behavior

    Description: Creating a custom directive to manage checkbox selection in mat-selection-list as per radio button behavior.

    Code:

    // custom-radio-behavior.directive.ts import { Directive, HostListener, ElementRef } from '@angular/core'; import { MatListOption } from '@angular/material/list'; @Directive({ selector: '[appCustomRadioBehavior]' }) export class CustomRadioBehaviorDirective { constructor(private el: ElementRef<HTMLElement>) {} @HostListener('click', ['$event']) onClick(event: MouseEvent) { const options = this.el.nativeElement.closest('mat-selection-list').querySelectorAll('mat-list-option') as MatListOption[]; options.forEach(o => o.selected = false); (this.el.nativeElement as MatListOption).selected = true; } } 
    <!-- usage in component --> <mat-selection-list> <mat-list-option checkboxPosition="before" appCustomRadioBehavior> Option 1 </mat-list-option> <mat-list-option checkboxPosition="before" appCustomRadioBehavior> Option 2 </mat-list-option> <mat-list-option checkboxPosition="before" appCustomRadioBehavior> Option 3 </mat-list-option> </mat-selection-list> 
  9. Angular mat-selection-list single checkbox with FormGroup and FormBuilder

    Description: Using FormGroup and FormBuilder to manage checkbox selection in mat-selection-list as per radio button behavior.

    Code:

    // component.ts import { Component } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; @Component({ selector: 'app-radio-like-checkboxes', templateUrl: './radio-like-checkboxes.component.html', styleUrls: ['./radio-like-checkboxes.component.css'] }) export class RadioLikeCheckboxesComponent { form: FormGroup; constructor(private fb: FormBuilder) { this.form = this.fb.group({ selectedOption: null }); } } 
    <!-- component.html --> <form [formGroup]="form"> <mat-selection-list formControlName="selectedOption"> <mat-list-option checkboxPosition="before" [value]="1"> Option 1 </mat-list-option> <mat-list-option checkboxPosition="before" [value]="2"> Option 2 </mat-list-option> <mat-list-option checkboxPosition="before" [value]="3"> Option 3 </mat-list-option> </mat-selection-list> </form> 

More Tags

nsuserdefaults nav-pills angular-observable non-ascii-characters intel-edison sap-commerce-cloud jekyll eslint supertest accord.net

More Programming Questions

More Statistics Calculators

More Organic chemistry Calculators

More Financial Calculators

More Date and Time Calculators