Grouping in EJ2 TypeScript Combo box control
2 May 20238 minutes to read
The ComboBox supports wrapping nested elements into a group based on different categories. The category of each list item can be mapped through the groupBy field in the data table. The group header is displayed both as inline and fixed headers. The fixed group header content is updated dynamically on scrolling the popup list with its category value.
In the following sample, vegetables are grouped according on its category using groupBy
field.
import { ComboBox } from '@syncfusion/ej2-dropdowns';
//define the data with category
let vegetableData: { [key: string]: Object }[] = [
{ Vegetable: 'Chickpea', Category: 'Beans', Id: '1' },
{ Vegetable: 'Green bean', Category: 'Beans', Id: '2' },
{ Vegetable: 'Spinach', Category: 'Leafy and Salad', Id: '3' },
{ Vegetable: 'Horse gram', Category: 'Beans', Id: '4' },
{ Vegetable: 'Cabbage', Category: 'Leafy and Salad', Id: '5' },
{ Vegetable: 'Wheat grass', Category: 'Leafy and Salad', Id: '6' },
{ Vegetable: 'Yarrow', Category: 'Leafy and Salad', Id: '7' }
];
//initiate the ComboBox
let vegetables: ComboBox = new ComboBox({
//set the grouped data to dataSource property
dataSource: vegetableData,
// map the groupBy field with Category column
fields: { groupBy: 'Category', text: 'Vegetable', value: 'Id' },
// set the placeholder to the ComboBox input
placeholder: "Select a vegetable",
// Set the popup list height
popupHeight: '200px'
});
//render the ComboBox component
vegetables.appendTo('#comboelement');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 ComboBox</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/27.2.2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-dropdowns/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:50px auto 0; width:250px;">
<br>
<input type="text" id='comboelement' />
</div>
</body>
</html>
HTML select
The ComboBox also supports grouping of list items under specific groups by initiating the <select>
element using optgroup
. The nested items are wrapped based on the <optgroup>
tag that is presents in the <select>
element
<select id="selectElement">
<optgroup label="Beans">
<option value="1">Chickpea</option>
<option value="2">Green bean</option>
<option value="3" selected="selected">Horse gram</option>
</optgroup>
<optgroup label="Leafy and Salad">
<option value="4">Spinach</option>
<option value="5">Cabbage</option>
<option value="6">Wheat grass</option>
<option value="7">Yarrow</option>
</optgroup>
</select>
import { ComboBox } from '@syncfusion/ej2-dropdowns';
// initialize ComboBox component
let ComboBoxObject: ComboBox = new ComboBox({
placeholder:"Select a vegetable",
popupHeight: "200px"
});
// render initialized ComboBox
ComboBoxObject.appendTo('#selectElement');
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 ComboBox</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/27.2.2/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-dropdowns/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:50px auto 0; width:250px;">
<br>
<select id="selectElement">
<optgroup label="Beans">
<option value="1">Chickpea</option>
<option value="2">Green bean</option>
<option value="3" selected="selected">Horse gram</option>
</optgroup>
<optgroup label="Leafy and Salad">
<option value="5">Cabbage</option>
<option value="4">Spinach</option>
<option value="6">Wheat grass</option>
<option value="7">Yarrow</option>
</optgroup>
</select>
</div>
</body>
</html>
Customization
The grouping header is also provided with customization option. This allows custom designing using the groupTemplate
property for both inline and fixed headers.