New to Telerik UI for Blazor? Start a free 30-day trial
MultiColumnComboBox with Grid
Environment
Product |
MultiColumnComboBox for Blazor, Grid for Blazor, Popup for Blazor |
Description
This KB answers the following questions:
- How to add more Grid features to the MultiColumnComboBox?
- How to filter by multiple columns in the MultiColumnComboBox Grid?
- How to enable column resizing in the MultiColumnComboBox?
Solution
The steps and example below describe how to create a custom component that looks and works similarly to a Telerik MultiColumnComboBox for Blazor, but uses an actual Grid component inside a Popup component.
Prerequisites
The solution below requires familiarity with:
- Telerik Grid for Blazor, especially the Grid state and Grid selection.
- Telerik Popup for Blazor;
- Telerik TextBox for Blazor;
- Implementing Blazor component parameters that support two-way binding.
- Implementing Blazor components with
ChildContent
that is aRenderFragment
.
Steps
- Implement UI that looks like a closed Telerik ComboBox. For example, use a TextBox and a
TextBoxSuffixTemplate
with an icon Button inside. - Add a Popup and set its
AnchorSelector
to be a selector that depends on a customClass
of the TextBox. - Add a Grid inside the Popup. Enable the features that you need.
- Use Grid row selection and the Grid
SelectedItemsChanged
event to set the TextBoxValue
and the overall value of the custom MultiColumnComboBox component. - (optional) Extract the whole implementation to a separate generic Razor component and implement parameters such as
Value
andData
.
How it Works
- The example demonstrates a generic and reusable Razor component called
ComboBoxGrid
. - The component has several parameters, which work similarly to the corresponding MultiColumnComboBox parameters:
Data
Value
TextField
ValueField
Width
- Typing in the component's textbox opens the dropdown and filters the Grid, similar to a SearchBox.
- Blurring the textbox or hitting Enter will select the first matching item in the Grid.
- Selecting a Grid item applies a new
ComboBoxGrid
value and closes the dropdown. - Closing and reopening the dropdown preserves the Grid state.
This KB article shows a custom integration scenario that uses built-in features and APIs of Telerik UI for Blazor components. The implementation is provided as is. It can be subject to a lot of changes and customizations by the developer, according to the business requirements.
Example
Custom MultiColumnComboBox with an actual Grid component in the dropdown
RAZOR
<p>Custom MultiComboBox Value: @CustomComboBoxValue</p>
<ComboBoxGrid Data="@GridData"
@bind-Value="@CustomComboBoxValue"
ValueField="@nameof(SampleModel.Id)"
TextField="@nameof(SampleModel.Name)"
Width="300px">
<GridColumn Field="@nameof(SampleModel.Name)" Title="Product Name" />
<GridColumn Field="@nameof(SampleModel.Price)" Width="120px" DisplayFormat="{0:c2}" />
<GridColumn Field="@nameof(SampleModel.Quantity)" Width="120px" />
</ComboBoxGrid>
@code {
private List<SampleModel> GridData { get; set; } = new();
private int CustomComboBoxValue { get; set; } = 3;
protected override void OnInitialized()
{
for (int i = 1; i <= 37; i++)
{
GridData.Add(new SampleModel()
{
Id = i,
Name = $"Name {i}",
Price = Random.Shared.Next(1, 100) * 1.23m,
Quantity = Random.Shared.Next(0, 1000)
});
}
}
public class SampleModel
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public int Quantity { get; set; }
}
}