Grouping in Dropdown List

28 May 20243 minutes to read

The DropDownList supports wrapping of the nested elements into a group based on different categories. The category of each list item can be mapped through the DropDownListFieldSettings.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.

To get started quickly with grouping in the Blazor DropDown List component, you can check the video below.

In the following sample, vegetables are grouped according to their category using the DropDownListFieldSettings.GroupBy field.

  • CSHTML
  • @using Syncfusion.Blazor.DropDowns
    
    <SfDropDownList TValue="string" TItem="Vegetables" Placeholder="e.g. Select a vegetable" DataSource="@LocalData">
        <DropDownListFieldSettings GroupBy="Category" Value="Vegetable"></DropDownListFieldSettings>
    </SfDropDownList>
    
    @code {
    
        public IEnumerable<Vegetables> LocalData { get; set; } = new Vegetables().VegetablesList();
    
        public class Vegetables
        {
            public string Vegetable { get; set; }
            public string Category { get; set; }
            public string ID { get; set; }
    
            public List<Vegetables> VegetablesList()
            {
                List<Vegetables> Veg = new List<Vegetables>();
                Veg.Add(new Vegetables { Vegetable = "Cabbage", Category = "Leafy and Salad", ID = "item1" });
                Veg.Add(new Vegetables { Vegetable = "Chickpea", Category = "Beans", ID = "item2" });
                Veg.Add(new Vegetables { Vegetable = "Garlic", Category = "Bulb and Stem", ID = "item3" });
                Veg.Add(new Vegetables { Vegetable = "Green bean", Category = "Beans", ID = "item4" });
                Veg.Add(new Vegetables { Vegetable = "Horse gram", Category = "Beans", ID = "item5" });
                Veg.Add(new Vegetables { Vegetable = "Nopal", Category = "Bulb and Stem", ID = "item6" });
                Veg.Add(new Vegetables { Vegetable = "Onion", Category = "Bulb and Stem", ID = "item7" });
                Veg.Add(new Vegetables { Vegetable = "Pumpkins", Category = "Leafy and Salad", ID = "item8" });
                Veg.Add(new Vegetables { Vegetable = "Spinach", Category = "Leafy and Salad", ID = "item9" });
                Veg.Add(new Vegetables { Vegetable = "Wheat grass", Category = "Leafy and Salad", ID = "item10" });
                Veg.Add(new Vegetables { Vegetable = "Yarrow", Category = "Leafy and Salad", ID = "item11" });
                return Veg;
            }
        }
    }

    Grouping in Blazor DropdownList

    Fixed group header

    Classify the items based on the DropDownListFieldSettings.GroupBy field mapped to the control. The headers of group items will be fixed at the top till their belonging items ends up on scrolling.

    Group header template

    The group header title under which appropriate sub-items are categorized is customize with the help of the GroupTemplate property. This template is common for both inline and floating group HeaderTemplate.

    Limitations of grouping

    Grouping feature has some limitations. These are:

    • The component will support only single level of grouping, and you cannot specify it as like TreeView component.