How can I help you?
Grouping in React Multi select component
21 Feb 202624 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 as you scroll through the popup list, displaying the category value for each group.
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'));Customization
The grouping header can be customized using the groupTemplate property. This allows you to design custom headers for both inline and fixed group display modes, providing flexible control over group header appearance and content.
Grouping with CheckBox
The MultiSelect now supports rendering checkboxes in group headers, allowing you to select all items within a group in a single action. Enable this feature by setting the enableGroupCheckBox property to true and configuring the mode property as CheckBox.
Inject the CheckBoxSelection module in the MultiSelect to use the 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'));