Skip to content

Commit 7b6114d

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

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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](slug: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+
private List<Person> AllData { get; set; }
81+
private async Task GetRemoteData(ComboBoxReadEventArgs args)
82+
{
83+
IsLoading = true;
84+
ComboBoxRef?.Refresh();
85+
await LoadData();
86+
87+
var result = AllData.ToDataSourceResult(args.Request);
88+
89+
// set the Data and the TotalItems to the current page of data and total number of items
90+
args.Data = result.Data;
91+
args.Total = result.Total;
92+
IsLoading = false;
93+
ComboBoxRef?.Refresh();
94+
}
95+
96+
private async Task<Person?> GetModelFromValue(int selectedValue)
97+
{
98+
await Task.Delay(400); // simulate real network and database delays. Remove in a real app
99+
100+
return AllData.FirstOrDefault(x => selectedValue == x.Id);
101+
}
102+
103+
private async Task LoadData()
104+
{
105+
await Task.Delay(1500);
106+
if (AllData == null)
107+
{
108+
AllData = Enumerable.Range(1, 12345).Select(x => new Person { Id = x, Name = $"Name {x}" }).ToList();
109+
}
110+
}
111+
112+
public class Person
113+
{
114+
public int Id { get; set; }
115+
public string Name { get; set; }
116+
}
117+
}
118+
```
119+
120+
### Key Points
121+
- Use the `HeaderTemplate` for displaying a loading indicator during scrolling or filtering.
122+
- Call the `Refresh` method on the ComboBox reference to update the UI dynamically during data load operations.
123+
- Toggle visibility of old items using CSS to enhance user experience.
124+
125+
## See Also
126+
- [ComboBox HeaderTemplate Documentation](slug:components/combobox/templates#header-template)
127+
- [ComboBox Reference and Methods](slug:components/combobox/overview#combobox-reference-and-methods)
128+
- [ComboBox Virtualization Documentation](slug:combobox-virtualization#remote-data-example)

0 commit comments

Comments
 (0)