Selection in Angular List box component
27 Apr 20247 minutes to read
The ListBox provides support to select an item or a group of item by mouse or keyboard action. There are two selection modes available in list box,
- Single - To select single item in the list box.
- Multiple - To select multiple items in the list box.
On selection of each list box item, change event is triggered.
Single selection
To enable single selection in the list box, 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';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Multiple selection
To enable multiple selection in the list box, 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';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Checkbox selection
The ListBox supports checkbox in default and grouped list box 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 list box, 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';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));To select all the items in the list box,
selectAllmethod can also be used.