Grouping in React MultiSelect Dropdown
The React 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 as you scroll through the popup list, displaying the category value for each group.
Map the groupBy field through the fields prop to define which property of the data source represents the group category:
In the following sample, vegetables are grouped according to their category using the groupBy field.
[Class-component]
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
// define the data with category
vegetableData = [
{ 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
fields = { groupBy: 'category', text: 'vegetable', value: 'id' };
render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" popupHeight='200px' fields={this.fields} dataSource={this.vegetableData} placeholder="Select a vegetable"/>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component<{}, {}> {
// define the data with category
private 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
private fields: object = { groupBy: 'category', text: 'vegetable', value: 'id' };
public render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" popupHeight='200px' fields={this.fields} dataSource={this.vegetableData} placeholder="Select a vegetable" />
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));[Functional-component]
import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the data with category
let vegetableData = [
{ 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
const fields = { groupBy: 'category', text: 'vegetable', value: 'id' };
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" popupHeight='200px' fields={fields} dataSource={vegetableData} placeholder="Select a vegetable"/>);
}
ReactDOM.render(<App />, document.getElementById('sample'));import { MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the data with category
let 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
const fields: object = { groupBy: 'category', text: 'vegetable', value: 'id' };
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" popupHeight='200px' fields={fields} dataSource={vegetableData} placeholder="Select a vegetable" />
);
}
ReactDOM.render(<App />, document.getElementById('sample'));Customize the group header
The grouping header can be customized using the groupTemplate property. The custom template applies to both inline and fixed group display modes, providing flexible control over group header appearance and content. See the Group Template section for a usage example.
Grouping with checkbox
The React MultiSelect now supports rendering checkboxes in group headers, allowing you to select all items within a group in a single action. Inject the CheckBoxSelection module in the React MultiSelect to use checkbox selection.
Then enable this feature by setting the enableGroupCheckBox property to true and configuring the mode property as CheckBox.
[Class-component]
import { CheckBoxSelection, Inject, MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
// define the data with category
vegetableData = [
{ 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
fields = { groupBy: 'category', text: 'vegetable', value: 'id' };
render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" popupHeight='200px' fields={this.fields} dataSource={this.vegetableData} placeholder="Select a vegetable" mode="CheckBox" enableGroupCheckBox="true" allowFiltering="true" showSelectAll="true" filterBarPlaceholder="Search Vegetables">
<Inject services={[CheckBoxSelection]}/>
</MultiSelectComponent>);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));import { CheckBoxSelection, Inject, MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component<{}, {}> {
// define the data with category
private 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
private fields: object = { groupBy: 'category', text: 'vegetable', value: 'id' };
public render() {
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" popupHeight='200px' fields={this.fields} dataSource={this.vegetableData} placeholder="Select a vegetable" mode="CheckBox" enableGroupCheckBox="true" allowFiltering="true" showSelectAll="true" filterBarPlaceholder="Search Vegetables">
<Inject services={[CheckBoxSelection]} />
</MultiSelectComponent>
);
}
}
ReactDOM.render(<App />, document.getElementById('sample'));[Functional-component]
import { CheckBoxSelection, Inject, MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the data with category
const vegetableData = [
{ 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
const fields = { groupBy: 'category', text: 'vegetable', value: 'id' };
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" popupHeight='200px' fields={fields} dataSource={vegetableData} placeholder="Select a vegetable" mode="CheckBox" enableGroupCheckBox="true" allowFiltering="true" showSelectAll="true" filterBarPlaceholder="Search Vegetables">
<Inject services={[CheckBoxSelection]}/>
</MultiSelectComponent>);
}
ReactDOM.render(<App />, document.getElementById('sample'));import { CheckBoxSelection, Inject, MultiSelectComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// define the data with category
const 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
const fields: object = { groupBy: 'category', text: 'vegetable', value: 'id' };
return (
// specifies the tag for render the MultiSelect component
<MultiSelectComponent id="mtselement" popupHeight='200px' fields={fields} dataSource={vegetableData} placeholder="Select a vegetable" mode="CheckBox" enableGroupCheckBox="true" allowFiltering="true" showSelectAll="true" filterBarPlaceholder="Search Vegetables">
<Inject services={[CheckBoxSelection]} />
</MultiSelectComponent>
);
}
ReactDOM.render(<App />, document.getElementById('sample'));