Skip to content

Commit 343e039

Browse files
authored
Convert Blazor Virtualize examples (#19963)
1 parent 990c72e commit 343e039

1 file changed

Lines changed: 53 additions & 66 deletions

File tree

aspnetcore/blazor/components/virtualization.md

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,73 @@ description: Learn how to use component virtualization in ASP.NET Core Blazor ap
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 09/21/2020
8+
ms.date: 09/22/2020
99
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: blazor/components/virtualization
1111
---
1212
# ASP.NET Core Blazor component virtualization
1313

1414
By [Daniel Roth](https://github.com/danroth27)
1515

16-
Improve the perceived performance of component rendering using the Blazor framework's built-in virtualization support. Virtualization is a technique for limiting UI rendering to just the parts that are currently visible. For example, virtualization is helpful when the app must render a long list or a table with many rows and only a subset of items is required to be visible at any given time. Blazor provides the `Virtualize` component that can be used to add virtualization to an app's components.
16+
Improve the perceived performance of component rendering using the Blazor framework's built-in virtualization support. Virtualization is a technique for limiting UI rendering to just the parts that are currently visible. For example, virtualization is helpful when the app must render a long list of items and only a subset of items is required to be visible at any given time. Blazor provides the `Virtualize` component that can be used to add virtualization to an app's components.
1717

1818
::: moniker range=">= aspnetcore-5.0"
1919

20-
Without virtualization, a typical list or table-based component might use a C# [`foreach`](/dotnet/csharp/language-reference/keywords/foreach-in) loop to render each item in the list or each row in the table:
20+
Without virtualization, a typical list might use a C# [`foreach`](/dotnet/csharp/language-reference/keywords/foreach-in) loop to render each item in the list:
2121

2222
```razor
23-
<table>
24-
@foreach (var employee in employees)
25-
{
26-
<tr>
27-
<td>@employee.FirstName</td>
28-
<td>@employee.LastName</td>
29-
<td>@employee.JobTitle</td>
30-
</tr>
31-
}
32-
</table>
23+
@foreach (var employee in employees)
24+
{
25+
<p>
26+
@employee.FirstName @employee.LastName has the
27+
job title of @employee.JobTitle.
28+
</p>
29+
}
3330
```
3431

3532
If the list contains thousands of items, then rendering the list may take a long time. The user may experience a noticeable UI lag.
3633

3734
Instead of rendering each item in the list all at one time, replace the [`foreach`](/dotnet/csharp/language-reference/keywords/foreach-in) loop with the `Virtualize` component and specify a fixed item source with `Items`. Only the items that are currently visible are rendered:
3835

3936
```razor
40-
<table>
41-
<Virtualize Context="employee" Items="@employees">
42-
<tr>
43-
<td>@employee.FirstName</td>
44-
<td>@employee.LastName</td>
45-
<td>@employee.JobTitle</td>
46-
</tr>
47-
</Virtualize>
48-
</table>
37+
<Virtualize Context="employee" Items="@employees">
38+
<p>
39+
@employee.FirstName @employee.LastName has the
40+
job title of @employee.JobTitle.
41+
</p>
42+
</Virtualize>
4943
```
5044

5145
If not specifying a context to the component with `Context`, use the `context` value (`@context.{PROPERTY}`) in the item content template:
5246

5347
```razor
54-
<table>
55-
<Virtualize Items="@employees">
56-
<tr>
57-
<td>@context.FirstName</td>
58-
<td>@context.LastName</td>
59-
<td>@context.JobTitle</td>
60-
</tr>
61-
</Virtualize>
62-
</table>
48+
<Virtualize Items="@employees">
49+
<p>
50+
@context.FirstName @context.LastName has the
51+
job title of @context.JobTitle.
52+
</p>
53+
</Virtualize>
6354
```
6455

6556
The `Virtualize` component calculates how many items to render based on the height of the container and the size of the rendered items.
6657

58+
The item content for the `Virtualize` component can include:
59+
60+
* Plain HTML and Razor code, as the preceding example shows.
61+
* One or more Razor components.
62+
* A mix of HTML/Razor and Razor components.
63+
6764
## Item provider delegate
6865

6966
If you don't want to load all of the items into memory, you can specify an items provider delegate method to the component's `ItemsProvider` parameter that asynchronously retrieves the requested items on demand:
7067

7168
```razor
72-
<table>
73-
<Virtualize Context="employee" ItemsProvider="@LoadEmployees">
74-
<tr>
75-
<td>@employee.FirstName</td>
76-
<td>@employee.LastName</td>
77-
<td>@employee.JobTitle</td>
78-
</tr>
79-
</Virtualize>
80-
</table>
69+
<Virtualize Context="employee" ItemsProvider="@LoadEmployees">
70+
<p>
71+
@employee.FirstName @employee.LastName has the
72+
job title of @employee.JobTitle.
73+
</p>
74+
</Virtualize>
8175
```
8276

8377
The items provider receives an `ItemsProviderRequest`, which specifies the required number of items starting at a specific start index. The items provider then retrieves the requested items from a database or other service and returns them as an `ItemsProviderResult<TItem>` along with a count of the total items. The items provider can choose to retrieve the items with each request or cache them so that they're readily available. Don't attempt to use an items provider and assign a collection to `Items` for the same `Virtualize` component.
@@ -101,46 +95,39 @@ private async ValueTask<ItemsProviderResult<Employee>> LoadEmployees(
10195
Because requesting items from a remote data source might take some time, you have the option to render a placeholder (`<Placeholder>...</Placeholder>`) until the item data is available:
10296

10397
```razor
104-
<table>
105-
<Virtualize Context="employee" ItemsProvider="@LoadEmployees">
106-
<ItemContent>
107-
<tr>
108-
<td>@employee.FirstName</td>
109-
<td>@employee.LastName</td>
110-
<td>@employee.JobTitle</td>
111-
</tr>
112-
</ItemContent>
113-
<Placeholder>
114-
<tr>
115-
<td>Loading...</td>
116-
</tr>
117-
</Placeholder>
118-
</Virtualize>
119-
</table>
98+
<Virtualize Context="employee" ItemsProvider="@LoadEmployees">
99+
<ItemContent>
100+
<p>
101+
@employee.FirstName @employee.LastName has the
102+
job title of @employee.JobTitle.
103+
</p>
104+
</ItemContent>
105+
<Placeholder>
106+
<p>
107+
Loading&hellip;
108+
</p>
109+
</Placeholder>
110+
</Virtualize>
120111
```
121112

122113
## Item size
123114

124115
The size of each item in pixels can be set with `ItemSize` (default: 50px):
125116

126117
```razor
127-
<table>
128-
<Virtualize Context="employee" Items="@employees" ItemSize="25">
129-
...
130-
</Virtualize>
131-
</table>
118+
<Virtualize Context="employee" Items="@employees" ItemSize="25">
119+
...
120+
</Virtualize>
132121
```
133122

134123
## Overscan count
135124

136125
`OverscanCount` determines how many additional items are rendered before and after the visible region. This setting helps to reduce the frequency of rendering during scrolling. However, higher values result in more elements rendered in the page (default: 3):
137126

138127
```razor
139-
<table>
140-
<Virtualize Context="employee" Items="@employees" OverscanCount="4">
141-
...
142-
</Virtualize>
143-
</table>
128+
<Virtualize Context="employee" Items="@employees" OverscanCount="4">
129+
...
130+
</Virtualize>
144131
```
145132

146133
::: moniker-end

0 commit comments

Comments
 (0)