Selection in EJ2 TypeScript List box control
13 Dec 20249 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
inselectionSettings
property.
import { ListBox } from '@syncfusion/ej2-dropdowns';
// define the array of object
let 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' }
];
// initialize ListBox component
let listObj: ListBox = new ListBox({
//set the data to dataSource property
dataSource: data,
selectionSettings: {
mode: 'Single'
}
});
listObj.appendTo('#listbox');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 ListBox</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:0 auto; width:250px;">
<h4>Select your favorite car:</h4>
<input id='listbox' />
</div>
</body>
</html>
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 { ListBox } from '@syncfusion/ej2-dropdowns';
// define the array of object
let 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' }
];
// initialize ListBox component
let listObj: ListBox = new ListBox({
//set the data to dataSource property
dataSource: data,
selectionSettings: {
mode: 'Multiple'
}
});
listObj.appendTo('#listbox');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 ListBox</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:0 auto; width:250px;">
<h4>Select your favorite car:</h4>
<input id='listbox' />
</div>
</body>
</html>
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 { ListBox, CheckBoxSelection } from '@syncfusion/ej2-dropdowns';
ListBox.Inject(CheckBoxSelection);
// define the array of object
let 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' }
];
// initialize ListBox component
let listObj: ListBox = new ListBox({
//set the data to dataSource property
dataSource: data,
selectionSettings: {
showCheckbox: true,
showSelectAll: true
}
});
listObj.appendTo('#listbox');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 ListBox</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="styles.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container' style="margin:0 auto; width:250px;">
<h4>Select your favorite car:</h4>
<input id='listbox' />
</div>
</body>
</html>
To select all the items in the list box,
selectAll
method can also be used.