Selection in Angular ListBox
25 Sep 20267 minutes to read
The Angular ListBox provides support to select an item or a group of item by mouse or keyboard action. There are two selection modes available in Angular ListBox,
- Single - To select single item in the list box.
- Multiple - To select multiple items in the list box.
On selection of each Angular ListBox item, change event is triggered.
Single selection
To enable single selection in the Angular ListBox, mode should be set as Single in selectionSettings property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { ListBoxComponent, ListBoxAllModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonComponent } from '@syncfusion/ej2-angular-buttons'
import { CheckBoxSelection } from '@syncfusion/ej2-angular-dropdowns';
import { Component } from '@angular/core';
(ListBoxComponent as any).Inject(CheckBoxSelection);
@Component({
imports: [
FormsModule, ReactiveFormsModule,ListBoxAllModule
],
standalone: true,
selector: 'app-container',
template: `<div class="e-section-control">
<ejs-listbox [dataSource]="data" [selectionSettings]="selection"></ejs-listbox></div>`
})
export class AppComponent{
public data: { [key: string]: Object }[] = [
{ text: 'Hennessey Venom' },
{ text: 'Bugatti Chiron' },
{ text: 'Bugatti Veyron Super Sport' },
{ text: 'SSC Ultimate Aero'},
{ text: 'Koenigsegg CCR'},
{ text: 'McLaren F1' },
{ text: 'Aston Martin One- 77' },
{ text: 'Jaguar XJ220' },
{ text: 'McLaren P1' },
{ text: 'Ferrari LaFerrari' }
];
public selection = { mode: "Single" }
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Multiple selection
To enable multiple selection in the Angular ListBox, mode should be set as Multiple in selectionSettings property.
To select multiple items, use the SHIFT, CTRL, and arrow keys to make selections.
By default, the selection mode is set as
Multiple.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { ListBoxComponent, ListBoxAllModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonComponent } from '@syncfusion/ej2-angular-buttons'
import { CheckBoxSelection } from '@syncfusion/ej2-angular-dropdowns';
import { Component } from '@angular/core';
(ListBoxComponent as any).Inject(CheckBoxSelection);
@Component({
imports: [
FormsModule, ReactiveFormsModule, ListBoxAllModule
],
standalone: true,
selector: 'app-container',
template: `<div class="e-section-control">
<ejs-listbox [dataSource]="data" [selectionSettings]="selection"></ejs-listbox></div>`
})
export class AppComponent{
public data: { [key: string]: Object }[] = [
{ text: 'Hennessey Venom' },
{ text: 'Bugatti Chiron' },
{ text: 'Bugatti Veyron Super Sport' },
{ text: 'SSC Ultimate Aero'},
{ text: 'Koenigsegg CCR'},
{ text: 'McLaren F1' },
{ text: 'Aston Martin One- 77' },
{ text: 'Jaguar XJ220' },
{ text: 'McLaren P1' },
{ text: 'Ferrari LaFerrari' }
];
public selection = {
mode: "Multiple"
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Checkbox selection
The Angular ListBox supports checkbox in default and grouped Angular ListBox which is used to select multiple items. CheckBox selection can be enabled by injecting CheckBoxSelection module and also showCheckBox property should be set as true.
Select All
To select all the items in the Angular ListBox, showSelectAll should be set as true.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { ListBoxComponent, ListBoxAllModule, CheckBoxSelection } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonComponent } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
ListBoxComponent.Inject(CheckBoxSelection);
@Component({
imports: [
FormsModule, ReactiveFormsModule,ListBoxAllModule
],
standalone: true,
selector: 'app-container',
template: `<div class="e-section-control">
<ejs-listbox [dataSource]="data" [selectionSettings]="selection"></ejs-listbox></div>`
})
export class AppComponent{
public data: { [key: string]: Object }[] = [
{ text: 'Hennessey Venom' },
{ text: 'Bugatti Chiron' },
{ text: 'Bugatti Veyron Super Sport' },
{ text: 'SSC Ultimate Aero'},
{ text: 'Koenigsegg CCR'},
{ text: 'McLaren F1' },
{ text: 'Aston Martin One- 77' },
{ text: 'Jaguar XJ220' },
{ text: 'McLaren P1' },
{ text: 'Ferrari LaFerrari' }
];
public selection = {
showCheckbox: true,
showSelectAll: true
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
bootstrapApplication(AppComponent).catch((err) => console.error(err));To select all the items in the Angular ListBox,
selectAllmethod can also be used.