Grouping in Angular Multi select component
27 Apr 20248 minutes to read
The MultiSelect 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 { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { MultiSelectModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component } from '@angular/core';
@Component({
imports: [
FormsModule, ReactiveFormsModule, MultiSelectModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the MultiSelect component
template: `<ejs-multiselect id='multiselectelement' [dataSource]='vegetableData' [fields]='fields'[placeholder]='placeholder' [popupHeight]='height'></ejs-multiselect>`
})
export class AppComponent {
constructor() {
}
//define the data with category
public vegetableData: { [key: string]: Object }[] = [
{ vegetable: 'Cabbage', category: 'Leafy and Salad', id: 'item1' },
{ vegetable: 'Spinach', category: 'Leafy and Salad', id: 'item2' },
{ vegetable: 'Wheat grass', category: 'Leafy and Salad', id: 'item3' },
{ vegetable: 'Yarrow', category: 'Leafy and Salad', id: 'item4' },
{ vegetable: 'Pumpkins', category: 'Leafy and Salad', id: 'item5' },
{ vegetable: 'Chickpea', category: 'Beans', id: 'item6' },
{ vegetable: 'Green bean', category: 'Beans', id: 'item7' },
{ vegetable: 'Horse gram', category: 'Beans', id: 'item8' },
{ vegetable: 'Garlic', category: 'Bulb and Stem', id: 'item9' },
{ vegetable: 'Nopal', category: 'Bulb and Stem', id: 'item10' },
{ vegetable: 'Onion', category: 'Bulb and Stem', id: 'item11' }
];
// map the groupBy field with category column
public fields: Object = { groupBy: 'category', text: 'vegetable', value: 'id' };
// Set the popup list height
public height: string = '200px';
// set the placeholder to the MultiSelect input
public placeholder: string = 'Select vegetables';
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Customization
The grouping header is also provided with customization option. This allows custom
designing using the groupTemplate
property for both inline and fixed headers.
Grouping with CheckBox
Previously, there is no checkbox for group headers. Now, this feature allow to render checkbox in group header to select the group items in single selection. You can enable this feature by setting enableGroupCheckBox
property value as true and mode property as CheckBox.
Inject the CheckBoxSelection
module in the MultiSelect to use the checkbox.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { MultiSelectModule } from '@syncfusion/ej2-angular-dropdowns'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { Component, OnInit } from '@angular/core';
import { CheckBoxSelectionService } from '@syncfusion/ej2-angular-dropdowns';
@Component({
imports: [
FormsModule, ReactiveFormsModule, MultiSelectModule, ButtonModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the MultiSelect component
template: `<ejs-multiselect id='multiselectelement' [dataSource]='vegetableData' [fields]='fields'[placeholder]='placeholder' [popupHeight]='height' [mode]='mode' [enableGroupCheckBox]='enableGroupCheckBox' [allowFiltering]='allowFiltering' [filterBarPlaceholder]='filterBarPlaceholder' [showSelectAll]='showSelectAll'></ejs-multiselect>`,
providers: [CheckBoxSelectionService]
})
export class AppComponent {
public mode?: string;
constructor() {
}
//define the data with category
public vegetableData: { [key: string]: Object }[] = [
{ vegetable: 'Cabbage', category: 'Leafy and Salad', id: 'item1' },
{ vegetable: 'Spinach', category: 'Leafy and Salad', id: 'item2' },
{ vegetable: 'Wheat grass', category: 'Leafy and Salad', id: 'item3' },
{ vegetable: 'Yarrow', category: 'Leafy and Salad', id: 'item4' },
{ vegetable: 'Pumpkins', category: 'Leafy and Salad', id: 'item5' },
{ vegetable: 'Chickpea', category: 'Beans', id: 'item6' },
{ vegetable: 'Green bean', category: 'Beans', id: 'item7' },
{ vegetable: 'Horse gram', category: 'Beans', id: 'item8' },
{ vegetable: 'Garlic', category: 'Bulb and Stem', id: 'item9' },
{ vegetable: 'Nopal', category: 'Bulb and Stem', id: 'item10' },
{ vegetable: 'Onion', category: 'Bulb and Stem', id: 'item11' }
];
// map the groupBy field with category column
public fields: Object = { groupBy: 'category', text: 'vegetable', value: 'id' };
// Set the popup list height
public height: string = '200px';
// set the placeholder to the MultiSelect input
public placeholder: string = 'Select vegetables';
// set value of enableGroupCheckBox to true
enableGroupCheckBox: boolean = true;
// set value of allowFiltering to true
allowFiltering: boolean = true;
// set the placeholder to the filterbar
filterBarPlaceholder: string = "Search Vegetables";
// set the value of showSelectAll as true
showSelectAll: boolean = true;
ngOnInit(): void {
// set the type of mode for checkbox to visualized the checkbox added in li element.
this.mode = 'CheckBox';
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));