Skip to content

Commit cf23fe7

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

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
title: Displaying 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+
meta_title: Adding Loader to ComboBox During Remote Data Fetch and Virtualization
7+
slug: adding-loader-combobox-remote-data-virtualization
8+
tags: combobox, ui-for-blazor, templates, nodatatemplate, header-template, loader
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 UI for Blazor</td>
20+
</tr>
21+
<tr>
22+
<td>Version</td>
23+
<td>Current</td>
24+
</tr>
25+
</tbody>
26+
</table>
27+
28+
## Description
29+
30+
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.
31+
32+
## Solution
33+
34+
To address the missing loading indicator issue in the TelerikComboBox component, follow these steps:
35+
36+
### 1. Display a Loading Indicator During Remote Data Fetch
37+
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.
38+
39+
### 2. Clear Old Items During Filtering/Search
40+
Modify the visibility of old items using CSS rules and conditionally toggle their appearance based on the loading state.
41+
42+
### Implementation
43+
44+
Below is an example implementation that solves both issues:
45+
46+
```razor
47+
@using Telerik.DataSource
48+
@using Telerik.DataSource.Extensions
49+
50+
<p>@SelectedValue</p>
51+
52+
@if (IsLoading == true) {
53+
<style>
54+
.example-cb .k-list-item {
55+
visibility: hidden;
56+
pointer-events: none;
57+
}
58+
59+
.example-cb .k-nodata {
60+
display: none;
61+
}
62+
</style>
63+
}
64+
<TelerikComboBox @ref="ComboBoxRef" TItem="@Person" TValue="@int"
65+
ScrollMode="@DropDownScrollMode.Virtual"
66+
OnRead="@GetRemoteData"
67+
ValueMapper="@GetModelFromValue"
68+
ItemHeight="30"
69+
PageSize="20"
70+
TextField="@nameof(Person.Name)"
71+
ValueField="@nameof(Person.Id)"
72+
@bind-Value="@SelectedValue"
73+
Filterable="true" FilterOperator="@StringFilterOperator.Contains">
74+
<ComboBoxSettings>
75+
<ComboBoxPopupSettings Class="example-cb" Height="200px" />
76+
</ComboBoxSettings>
77+
<NoDataTemplate>
78+
</NoDataTemplate>
79+
<HeaderTemplate>
80+
<TelerikLoader Visible="@IsLoading" />
81+
</HeaderTemplate>
82+
</TelerikComboBox>
83+
84+
@code{
85+
bool IsLoading {get;set;} = false;
86+
int SelectedValue { get; set; } = 1234; // pre-select an item to showcase the value mapper
87+
private TelerikComboBox<Person, int>? ComboBoxRef { get; set; }
88+
async Task GetRemoteData(ComboBoxReadEventArgs args)
89+
{
90+
IsLoading = true;
91+
ComboBoxRef?.Refresh();
92+
DataEnvelope<Person> result = await MyService.GetItems(args.Request);
93+
94+
// set the Data and the TotalItems to the current page of data and total number of items
95+
args.Data = result.Data;
96+
args.Total = result.Total;
97+
IsLoading = false;
98+
ComboBoxRef?.Refresh();
99+
}
100+
101+
async Task<Person> GetModelFromValue(int selectedValue)
102+
{
103+
// return a model that matches the selected value so the component can get its text
104+
return await MyService.GetItemFromValue(selectedValue);
105+
}
106+
107+
// mimics a real service in terms of API appearance, refactor as necessary for your app
108+
public static class MyService
109+
{
110+
static List<Person> AllData { get; set; }
111+
112+
public static async Task<DataEnvelope<Person>> GetItems(DataSourceRequest request)
113+
{
114+
await Task.Delay(3000);
115+
if (AllData == null)
116+
{
117+
AllData = Enumerable.Range(1, 12345).Select(x => new Person { Id = x, Name = $"Name {x}" }).ToList();
118+
}
119+
120+
await Task.Delay(400); // simulate real network and database delays. Remove in a real app
121+
122+
var result = await AllData.ToDataSourceResultAsync(request);
123+
DataEnvelope<Person> dataToReturn = new DataEnvelope<Person>
124+
{
125+
Data = result.Data.Cast<Person>().ToList(),
126+
Total = result.Total
127+
};
128+
129+
return await Task.FromResult(dataToReturn);
130+
}
131+
132+
public static async Task<Person> GetItemFromValue(int selectedValue)
133+
{
134+
await Task.Delay(400); // simulate real network and database delays. Remove in a real app
135+
136+
return await Task.FromResult(AllData.FirstOrDefault(x => selectedValue == x.Id));
137+
}
138+
}
139+
140+
// used to showcase how you could simplify the return of more than one value from the service
141+
public class DataEnvelope<T>
142+
{
143+
public int Total { get; set; }
144+
public List<T> Data { get; set; }
145+
}
146+
147+
public class Person
148+
{
149+
public int Id { get; set; }
150+
public string Name { get; set; }
151+
}
152+
}
153+
```
154+
155+
### Key Points
156+
- Use the `HeaderTemplate` for displaying a loading indicator during scrolling or filtering.
157+
- Call the `Refresh` method on the ComboBox reference to update the UI dynamically during data load operations.
158+
- Toggle visibility of old items using CSS to enhance user experience.
159+
160+
## See Also
161+
- [ComboBox HeaderTemplate Documentation](https://www.telerik.com/blazor-ui/documentation/components/combobox/templates#header-template)
162+
- [ComboBox Reference and Methods](https://www.telerik.com/blazor-ui/documentation/components/combobox/overview#combobox-reference-and-methods)
163+
- [ComboBox Virtualization Documentation](https://www.telerik.com/blazor-ui/documentation/components/combobox/virtualization#remote-data-example)

0 commit comments

Comments
 (0)