Skip to content

Commit fa6a7ad

Browse files
committed
chore(example): Add new kb article combobox-virtualization-loader
1 parent 5a4d177 commit fa6a7ad

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
title: Display Loading Indicator in ComboBox with Remote Data and Virtualization
3+
description: Learn how to add a loading indicator in the TelerikComboBox component when using remote data and virtualization functionality in UI for Blazor.
4+
type: how-to
5+
page_title: Adding Loader to ComboBox During Remote Data Fetch and Virtualization
6+
slug: combobox-kb-virtualization-loader
7+
position:
8+
tags: blazor, combobox, loader, templates, nodatatemplate, headertemplate
9+
res_type: kb
10+
ticketid: 1693304
11+
---
12+
13+
## Environment
14+
15+
<table>
16+
<tbody>
17+
<tr>
18+
<td>Product</td>
19+
<td>ComboBox for Blazor</td>
20+
</tr>
21+
</tbody>
22+
</table>
23+
24+
## Description
25+
26+
When using the [ComboBox](https://www.telerik.com/blazor-ui/documentation/components/combobox/overview) component in UI for Blazor with remote data loading and virtualization, the dropdown briefly displays the "No data" message while waiting for the response. This behavior can confuse users who may assume there is no data available when it is still loading. Additionally, during virtual scrolling or filtering, the absence of a loading indicator can lead to user frustration as they cannot perceive ongoing data fetch operations.
27+
28+
## Solution
29+
30+
1. Display a Loading Indicator During Remote Data Fetch
31+
Use the `HeaderTemplate` property of the ComboBox component to show a loading indicator. Add a boolean flag to track the loading state and update it dynamically during data fetch operations. Use a CSS rule to disable the default "No Data" message.
32+
33+
2. Clear Old Items During Filtering/Search
34+
Modify the visibility of old items using CSS rules and conditionally toggle their appearance based on the loading state.
35+
36+
>caption Display a Loading Indicator When Using a Virtualized ComboBox
37+
38+
```razor
39+
@using Telerik.DataSource
40+
@using Telerik.DataSource.Extensions
41+
42+
<p>@SelectedValue</p>
43+
44+
@if (IsLoading == true) {
45+
<style>
46+
.example-cb .k-list-item {
47+
visibility: hidden;
48+
pointer-events: none;
49+
}
50+
51+
.example-cb .k-nodata {
52+
display: none;
53+
}
54+
</style>
55+
}
56+
<TelerikComboBox @ref="ComboBoxRef" TItem="@Person" TValue="@int"
57+
ScrollMode="@DropDownScrollMode.Virtual"
58+
OnRead="@GetRemoteData"
59+
ValueMapper="@GetModelFromValue"
60+
ItemHeight="30"
61+
PageSize="20"
62+
TextField="@nameof(Person.Name)"
63+
ValueField="@nameof(Person.Id)"
64+
@bind-Value="@SelectedValue"
65+
Filterable="true" FilterOperator="@StringFilterOperator.Contains">
66+
<ComboBoxSettings>
67+
<ComboBoxPopupSettings Class="example-cb" Height="200px" />
68+
</ComboBoxSettings>
69+
<NoDataTemplate>
70+
</NoDataTemplate>
71+
<HeaderTemplate>
72+
<TelerikLoader Visible="@IsLoading" />
73+
</HeaderTemplate>
74+
</TelerikComboBox>
75+
76+
@code{
77+
bool IsLoading {get;set;} = false;
78+
int SelectedValue { get; set; } = 1234; // pre-select an item to showcase the value mapper
79+
private TelerikComboBox<Person, int>? ComboBoxRef { get; set; }
80+
async Task GetRemoteData(ComboBoxReadEventArgs args)
81+
{
82+
IsLoading = true;
83+
ComboBoxRef?.Refresh();
84+
DataEnvelope<Person> result = await MyService.GetItems(args.Request);
85+
86+
// set the Data and the TotalItems to the current page of data and total number of items
87+
args.Data = result.Data;
88+
args.Total = result.Total;
89+
IsLoading = false;
90+
ComboBoxRef?.Refresh();
91+
}
92+
93+
async Task<Person> GetModelFromValue(int selectedValue)
94+
{
95+
// return a model that matches the selected value so the component can get its text
96+
return await MyService.GetItemFromValue(selectedValue);
97+
}
98+
99+
// mimics a real service in terms of API appearance, refactor as necessary for your app
100+
public static class MyService
101+
{
102+
static List<Person> AllData { get; set; }
103+
104+
public static async Task<DataEnvelope<Person>> GetItems(DataSourceRequest request)
105+
{
106+
await Task.Delay(500);
107+
if (AllData == null)
108+
{
109+
AllData = Enumerable.Range(1, 12345).Select(x => new Person { Id = x, Name = $"Name {x}" }).ToList();
110+
}
111+
112+
await Task.Delay(400); // simulate real network and database delays. Remove in a real app
113+
114+
var result = await AllData.ToDataSourceResultAsync(request);
115+
DataEnvelope<Person> dataToReturn = new DataEnvelope<Person>
116+
{
117+
Data = result.Data.Cast<Person>().ToList(),
118+
Total = result.Total
119+
};
120+
121+
return dataToReturn;
122+
}
123+
124+
public static async Task<Person?> GetItemFromValue(int selectedValue)
125+
{
126+
await Task.Delay(400); // simulate real network and database delays. Remove in a real app
127+
128+
return AllData.FirstOrDefault(x => selectedValue == x.Id);
129+
}
130+
}
131+
132+
// used to showcase how you could simplify the return of more than one value from the service
133+
public class DataEnvelope<T>
134+
{
135+
public int Total { get; set; }
136+
public List<T> Data { get; set; }
137+
}
138+
139+
public class Person
140+
{
141+
public int Id { get; set; }
142+
public string Name { get; set; }
143+
}
144+
}
145+
```
146+
147+
### Key Points
148+
- Use the `HeaderTemplate` for displaying a loading indicator during scrolling or filtering.
149+
- Call the `Refresh` method on the ComboBox reference to update the UI dynamically during data load operations.
150+
- Toggle visibility of old items using CSS to enhance user experience.
151+
152+
## See Also
153+
- [ComboBox HeaderTemplate Documentation](https://www.telerik.com/blazor-ui/documentation/components/combobox/templates#header-template)
154+
- [ComboBox Reference and Methods](https://www.telerik.com/blazor-ui/documentation/components/combobox/overview#combobox-reference-and-methods)
155+
- [ComboBox Virtualization Documentation](https://www.telerik.com/blazor-ui/documentation/components/combobox/virtualization#remote-data-example)

0 commit comments

Comments
 (0)