Grouping in Vue Multi select component

11 Jun 202412 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 contentis updated dynamically on scrolling the popup list with its category value.

To group the Vue MultiSelect dropdown items, you can check on this video:

In the following sample, vegetables are grouped according on its category using groupBy field.

<template>
  <div id="app">
    <div id='container' style="margin:50px auto 0; width:250px;">
      <br>
      <ejs-multiselect id='multiselect' :dataSource='vegetableData' :fields='fields' popupHeight="200px"
        placeholder="Select a vegetable"></ejs-multiselect>
    </div>
  </div>
</template>
<script setup>
import { MultiSelectComponent as EjsMultiselect } from "@syncfusion/ej2-vue-dropdowns";

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' }
];
const fields = { groupBy: 'category', text: 'vegetable', value: 'id' };

</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
</style>
<template>
  <div id="app">
    <div id='container' style="margin:50px auto 0; width:250px;">
      <br>
      <ejs-multiselect id='multiselect' :dataSource='vegetableData' :fields='fields' popupHeight="200px"
        placeholder="Select a vegetable"></ejs-multiselect>
    </div>
  </div>
</template>
<script>
import { MultiSelectComponent } from "@syncfusion/ej2-vue-dropdowns";

export default {
  name: "App",
  components: {
    "ejs-multiselect": MultiSelectComponent
  },
  data() {
    return {
      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' }
      ],
      fields: { groupBy: 'category', text: 'vegetable', value: 'id' }
    }
  }
}

</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
</style>

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.

<template>
  <div id="app">
    <div id='container' style="margin:50px auto 0; width:250px;">
      <br>
      <ejs-multiselect id='multiselect' :dataSource='vegetableData' :fields='fields' popupHeight="200px" mode="CheckBox"
        enableGroupCheckBox="true" allowFiltering="true" filterBarPlaceholder="Search Vegetables" showSelectAll="true"
        placeholder="Select vegetables"></ejs-multiselect>
    </div>
  </div>
</template>
<script setup>
import { provide } from "vue";
import { MultiSelectComponent as EjsMultiselect, CheckBoxSelection } from "@syncfusion/ej2-vue-dropdowns";

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' }
];
const fields = { groupBy: 'category', text: 'vegetable', value: 'id' };

provide('multiselect', [CheckBoxSelection]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
</style>
<template>
  <div id="app">
    <div id='container' style="margin:50px auto 0; width:250px;">
      <br>
      <ejs-multiselect id='multiselect' :dataSource='vegetableData' :fields='fields' popupHeight="200px" mode="CheckBox"
        enableGroupCheckBox="true" allowFiltering="true" filterBarPlaceholder="Search Vegetables" showSelectAll="true"
        placeholder="Select vegetables"></ejs-multiselect>
    </div>
  </div>
</template>
<script>
import { MultiSelectComponent, CheckBoxSelection } from "@syncfusion/ej2-vue-dropdowns";

export default {
  name: "App",
  components: {
    "ejs-multiselect": MultiSelectComponent
  },
  data() {
    return {
      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' }
      ],
      fields: { groupBy: 'category', text: 'vegetable', value: 'id' }
    }
  },
  provide: {
    multiselect: [CheckBoxSelection]
  }
}

</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
</style>

See Also