You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/components/virtualization.md
+1-217Lines changed: 1 addition & 217 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title: ASP.NET Core Blazor component virtualization
3
3
author: guardrex
4
4
description: Learn how to use component virtualization in ASP.NET Core Blazor apps.
5
-
monikerRange: '>= aspnetcore-3.1'
5
+
monikerRange: '>= aspnetcore-5.0'
6
6
ms.author: riande
7
7
ms.custom: mvc
8
8
ms.date: 10/02/2020
@@ -15,8 +15,6 @@ By [Daniel Roth](https://github.com/danroth27)
15
15
16
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.
17
17
18
-
::: moniker range=">= aspnetcore-5.0"
19
-
20
18
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:
21
19
22
20
```razor
@@ -130,220 +128,6 @@ The size of each item in pixels can be set with `ItemSize` (default: 50px):
130
128
</Virtualize>
131
129
```
132
130
133
-
::: moniker-end
134
-
135
-
::: moniker range="< aspnetcore-5.0"
136
-
137
-
For example, a grid or list that renders hundreds of rows containing components is processor intensive to render. Consider virtualizing a grid or list layout so that only a subset of the components is rendered at any given time.
138
-
139
-
The following `Virtualize` component (`Virtualize.cs`) implements <xref:Microsoft.AspNetCore.Components.ComponentBase> to render child content based on user scrolling:
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
335
-
}
336
-
}
337
-
```
338
-
339
-
In the preceding example, the approach is about avoiding absolute positioning for each individual item. Absolute positioning would have some advantages (we can be sure the items do take up the specified amount of Y space) but has the bad disadvantage that you lose the normal widths and can't get table columns to line up across rows/header when based on content sizing.
340
-
341
-
The concept behind the design of the `Virtualize` component is that the component doesn't change how the items are laid out within the DOM. There are no added wrapper elements, besides the single one whose `TagName` you specify.
342
-
343
-
The best approach is to avoid even the `TagName` wrapper element. Have the `Virtualize` component emit no elements of its own. All the component does is render however many of the template instances are required to fill up any remaining visible space in the closest scrollable ancestor, plus add a following spacer element that makes the scrollable ancestor have the right scrolling range. As far as the layout is concerned, it's the same as if the full range of children were physically in the DOM. It does require you to specify an accurate `ItemHeight` though. If you get it wrong (for example, because you're confused and think it's valid to specify `style.height` on a `<tr>`), then the component ends up rendering the wrong number of template instances and either not fill up the UI or inefficiently render too many. Additionally, the scroll range on the parent won't be correct.
344
-
345
-
::: moniker-end
346
-
347
131
## State changes
348
132
349
133
When making changes to items rendered by the `Virtualize` component, call <xref:Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged%2A> to force re-evaluation and rerendering of the component.
0 commit comments