TagBox - Data Shaping
- 6 minutes to read
Group Data
The Blazor TagBox allows you to organize list items into groups. This enhance user experience by allowing users to locate required values faster. To group data, specify the GroupFieldName property. Values from the specified field appear within group headers.
Note the following specifics:
- The TagBox supports only one group level.
- Users cannot collapse groups.
- Group headers do not take part in search operations.
- Multi-column TagBox controls also support groups.
- You can use the GroupHeaderDisplayTemplate property to customize group headers.
The following code organizes data items into groups based on the Country data source field:
@inject NwindDataService NwindDataService
<DxTagBox Data="@Data"
@bind-Values="@Values"
TextFieldName="@nameof(Customer.ContactName)"
GroupFieldName="@nameof(Customer.Country)"
SearchMode="@ListSearchMode.AutoSearch"
SearchFilterCondition="@ListSearchFilterCondition.Contains"
InputId="tbGrouping">
</DxTagBox>
@code {
IEnumerable<Customer> Data { get; set; }
IEnumerable<Customer> Values { get; set; }
protected override async Task OnInitializedAsync() {
Data = await NwindDataService.GetCustomersAsync();
Values = Data.Where(x => x.Country == "Argentina").Take(1);
}
}
Search and Filter Data
The DevExpress Blazor TagBox can search for text, and filter and highlight search results. Use the following API members to enable search and filter capabilities:
SearchMode — Specifies whether the component can search for text that users type in the edit box. When the AutoSearch mode is active, the TagBox filters its items based on the search string and highlights string matches. The search operation includes all visible columns.
Users can use special characters to create composite criteria. Refer to the following section for additional information: Search Syntax.
- SearchFilterCondition — Specifies the search and filter condition (Contains, Equals, or StartsWith).
- SearchTextParseMode — Specifies how the component combines words into the search query. If search text contains multiple words separated by spaces, words can be treated as single or individual conditions. The GroupWordsByAnd, GroupWordsByOr, and ExactMatch modes are available.
- SearchEnabled - Specifies whether the component can search text in cells of the current column.
<DxTagBox Data="Staff.DataSource"
@bind-Values="@Values"
TextFieldName="@nameof(Person.FirstName)"
ValueFieldName="@nameof(Person.Id)"
SearchMode="ListSearchMode.AutoSearch"
SearchFilterCondition="ListSearchFilterCondition.Contains">
<Columns>
<DxListEditorColumn FieldName="FirstName"></DxListEditorColumn>
<DxListEditorColumn FieldName="LastName"></DxListEditorColumn>
<DxListEditorColumn FieldName="Department" SearchEnabled="false"></DxListEditorColumn>
</Columns>
</DxTagBox>
@code{
IEnumerable<int> Values { get; set; }
}
When a user types text into the edit box, the TagBox filters and highlights search results.
Note
If you use the EditBoxDisplayTemplate property and need to enable filter mode, you should add a DxInputBox object to the template markup.
Disabled Items
You can disable individual items in the TagBox drop-down list. To do this, use the DisabledFieldName property. The property specifies a Boolean field that stores each item’s enabled or disabled state. Disabled items are grayed out and users cannot select them.
Note
Disabled items can improve component usability, but cannot replace adequate security measures. Users may still be able to select disabled items on the client side, so you should implement appropriate security checks in your code.
@inject NwindDataService NwindDataService
<DxListBox Data="@Products"
@bind-Values="@SelectedProducts"
TextFieldName="@nameof(Product.ProductName)"
DisabledFieldName="@nameof(Product.Discontinued)"
SelectionMode="ListBoxSelectionMode.Multiple"
CssClass="cw-400 chi-220">
</DxListBox>
@code {
IEnumerable<Product> Products { get; set; }
IEnumerable<Product> SelectedProducts { get; set; }
protected override async Task OnInitializedAsync() {
Products = await NwindDataService.GetProductsAsync();
SelectedProducts = Products.Take(1);
}
}