|
| 1 | +--- |
| 2 | +title: Virtual Scrolling |
| 3 | +page_title: Grid for Blazor | Virtual Scrolling |
| 4 | +description: Enable and configure virtual scrolling in Grid for Blazor |
| 5 | +slug: components/grid/virtual-scrolling |
| 6 | +tags: telerik,blazor,grid,virtual,scrolling |
| 7 | +published: True |
| 8 | +position: 55 |
| 9 | +--- |
| 10 | + |
| 11 | +# Virtual Scrolling |
| 12 | + |
| 13 | +Virtual scrolling is an alternative to paging. Instead of using a pager, the user scrolls vertically through all records in the data source. The same set of elements is reused to improve performance. While the new data is loading, a loading indicator is shown on the cells. |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +To enable virtual scrolling: |
| 18 | + |
| 19 | +1. Set `ScrollMode="@GridScrollMode.Virtual"` - this enables the virtualization of items |
| 20 | +2. Provide `Height`, `RowHeight`, and `PageSize` to the grid - this lets the grid calculate the position of the user in order to fetch the correct set of items from the data source. |
| 21 | + |
| 22 | +>caption Sample of virtual scrolling in the Telerik Grid for Blazor |
| 23 | +
|
| 24 | +````CSHTML |
| 25 | +@* Scroll the grid instead of paging *@ |
| 26 | +
|
| 27 | +<TelerikGrid Data=@GridData |
| 28 | + ScrollMode="@GridScrollMode.Virtual" |
| 29 | + Height="400px" RowHeight="40" PageSize="20" |
| 30 | + Sortable="true" FilterMode="@GridFilterMode.FilterMenu"> |
| 31 | + <GridColumns> |
| 32 | + <GridColumn Field="Id" /> |
| 33 | + <GridColumn Field="Name" Title="First Name" /> |
| 34 | + <GridColumn Field="LastName" Title="Last Name" /> |
| 35 | + <GridColumn Field="HireData"> |
| 36 | + <Template> |
| 37 | + @((context as SampleData).HireDate.ToString("MMMM dd, yyyy")) |
| 38 | + </Template> |
| 39 | + </GridColumn> |
| 40 | + </GridColumns> |
| 41 | +</TelerikGrid> |
| 42 | +
|
| 43 | +@code { |
| 44 | + public List<SampleData> GridData { get; set; } |
| 45 | +
|
| 46 | + protected override async Task OnInitializedAsync() |
| 47 | + { |
| 48 | + GridData = await GetData(); |
| 49 | + } |
| 50 | +
|
| 51 | + private async Task<List<SampleData>> GetData() |
| 52 | + { |
| 53 | + return Enumerable.Range(1, 1000).Select(x => new SampleData |
| 54 | + { |
| 55 | + Id = x, |
| 56 | + Name = $"name {x}", |
| 57 | + LastName = $"Surname {x}", |
| 58 | + HireDate = DateTime.Now.Date.AddDays(-x) |
| 59 | + }).ToList(); |
| 60 | + } |
| 61 | +
|
| 62 | + public class SampleData |
| 63 | + { |
| 64 | + public int Id { get; set; } |
| 65 | + public string Name { get; set; } |
| 66 | + public string LastName { get; set; } |
| 67 | + public DateTime HireDate { get; set; } |
| 68 | + } |
| 69 | +} |
| 70 | +```` |
| 71 | + |
| 72 | +>caption How virtual scrolling looks like (deliberately slowed down to showcase the loading signs) |
| 73 | +
|
| 74 | + |
| 75 | + |
| 76 | +## Notes |
| 77 | + |
| 78 | +There are several things to keep in mind when using virtual scrolling: |
| 79 | + |
| 80 | +* The `Height` of the grid must be in pixels. The `RowHeight` is a decimal value that is always considered as pixel values. |
| 81 | +* Do not mix virtualization with paging, as they are alternatives to the same feature. |
| 82 | +* You can control how many rows are rendered through the `PageSize`. If performance does not suit your needs, tweak mostly that property (for example, if latency is high - fetch larger chunks of data so that a remote service is called less often; or when the browser is responding slowly, decrease the page size to render fewer DOM elements). |
| 83 | +* When using the [`OnRead` event]({%slug components/grid/manual-operations%}), use the `PageSize` and `Skip` parameters to know what data to return, instead of `PageSize` and `Page` as with regular paging. |
| 84 | +* Horizontal scrolling is not virtualized, all columns are rendered. |
| 85 | +* Provide for a page size of the Grid that is large enough, so that the table rows do not fit in the scrollable data area, otherwise the vertical virtual scrollbar will not be created and scrolling will not work. The page size of the Grid should be over three times larger than the number of visible table rows in the data area. |
| 86 | + |
| 87 | +## Limitations |
| 88 | + |
| 89 | +Virtualization is mainly a technique for improving client-side performance and the user experience. Its cost is that some features of the grid do not work with it. An alternative to that is to use [regular paging]({%slug components/grid/features/paging%}) with [mantual data source operations]({%slug components/grid/manual-operations%}) to implement the desired performance of the data retrieval. |
| 90 | + |
| 91 | +List of the known limitations of the virtual scrolling feature: |
| 92 | + |
| 93 | +* [Hierarchy]({%slug components/grid/features/hierarchy%}) is not supported. |
| 94 | +* [Grouping]({%slug components/grid/features/grouping%}) is not supported. |
| 95 | +* [Row Templates]({%slug components/grid/features/templates%}#row-template) are not supported. |
| 96 | + |
| 97 | +## See Also |
| 98 | + |
| 99 | + * [Live Demo: Grid Sorting](https://demos.telerik.com/blazor-ui/grid/sorting) |
| 100 | + |
0 commit comments