@@ -27,58 +27,60 @@ To enable virtual scrolling:
2727> caption Sample of virtual scrolling in the Telerik Grid for Blazor
2828
2929```` CSHTML
30- @* Scroll the grid instead of paging *@
30+ @using Telerik.DataSource
31+ @using Telerik.DataSource.Extensions
3132
32- <TelerikGrid Data=@GridData
33+ <TelerikGrid OnRead="@OnGridRead"
34+ TItem="@Product"
3335 ScrollMode="@GridScrollMode.Virtual"
3436 Height="480px" RowHeight="60" PageSize="20"
35- Sortable="true" FilterMode="@GridFilterMode.FilterMenu ">
37+ Sortable="true" FilterMode="@GridFilterMode.FilterRow ">
3638 <GridColumns>
37- <GridColumn Field="Id" />
38- <GridColumn Field="Name" Title="First Name" />
39- <GridColumn Field="LastName" Title="Last Name" />
40- <GridColumn Field="HireDate" Width="200px">
41- <Template>
42- @((context as SampleData).HireDate.ToString("MMMM dd, yyyy"))
43- </Template>
44- </GridColumn>
39+ <GridColumn Field="@nameof(Product.Name)" Title="Product Name" />
40+ <GridColumn Field="@nameof(Product.Stock)" />
4541 </GridColumns>
4642</TelerikGrid>
4743
4844@code {
49- public List<SampleData > GridData { get; set; }
45+ private List<Product > GridData { get; set; } = new List<Product>();
5046
51- protected override async Task OnInitializedAsync( )
47+ private async Task OnGridRead(GridReadEventArgs args )
5248 {
53- GridData = await GetData();
49+ await Task.Delay(200); // simulate network delay
50+
51+ DataSourceResult result = GridData.ToDataSourceResult(args.Request);
52+
53+ args.Data = result.Data;
54+ args.Total = result.Total;
55+ args.AggregateResults = result.AggregateResults;
5456 }
5557
56- private async Task<List<SampleData>> GetData ()
58+ protected override void OnInitialized ()
5759 {
58- return Enumerable.Range(1, 1000).Select(x => new SampleData
60+ GridData = new List<Product>();
61+ var rnd = new Random();
62+
63+ for (int i = 1; i <= 1000; i++)
5964 {
60- Id = x,
61- Name = $"name {x}",
62- LastName = $"Surname {x}",
63- HireDate = DateTime.Now.Date.AddDays(-x)
64- }).ToList();
65+ GridData.Add(new Product()
66+ {
67+ Id = i,
68+ Name = $"Product {i}",
69+ Stock = rnd.Next(0, 100)
70+ });
71+ }
6572 }
6673
67- public class SampleData
74+ public class Product
6875 {
6976 public int Id { get; set; }
70- public string Name { get; set; }
71- public string LastName { get; set; }
72- public DateTime HireDate { get; set; }
77+ public string Name { get; set; } = string.Empty;
78+ public int Stock { get; set; }
7379 }
7480}
7581````
7682
77- > caption How virtual scrolling looks like (deliberately slowed down to showcase the loading placeholders)
78-
79- ![ Blazor Grid Virtual Scrolling Overview] ( images/virtual-scrolling-overview.gif )
80-
81- > tip The column where long text is expected (the ` Hire Date ` in this example) has a width set so that the text does not break into multiple lines and increase the height of the row. See the notes below for more details.
83+ > tip Set suitable widths for columns that will render long text. This will prevent the cell content from breaking into multiple lines, which will increase the row height. See the notes below for more details.
8284
8385## Notes
8486
@@ -124,64 +126,6 @@ List of the known limitations of the virtual scrolling feature:
124126
125127* When there are too many records, the browser may not let you scroll down to all of them, read more in the [ Virtual Scroll does not show all items] ({%slug grid-kb-virtualization-many-records%}) KB article.
126128
127- <!--
128- Code for the GIF
129-
130- <TelerikGrid TItem="@SampleData"
131- ScrollMode="@GridScrollMode.Virtual"
132- Height="480px" RowHeight="60" PageSize="20"
133- Sortable="true" FilterMode="@GridFilterMode.FilterMenu"
134- OnRead=@ReadItems Width="640px">
135- <GridColumns>
136- <GridColumn Field="Id" />
137- <GridColumn Field="Name" Title="First Name" />
138- <GridColumn Field="LastName" Title="Last Name" />
139- <GridColumn Field="HireData" Width="200px">
140- <Template>
141- @((context as SampleData).HireDate.ToString("MMMM dd, yyyy"))
142- </Template>
143- </GridColumn>
144- </GridColumns>
145- </TelerikGrid>
146-
147- @code {
148- public List<SampleData> SourceData { get; set; }
149-
150- protected override async Task OnInitializedAsync()
151- {
152- SourceData = await GetData();
153- }
154-
155- private async Task<List<SampleData>> GetData()
156- {
157- return Enumerable.Range(1, 1000).Select(x => new SampleData
158- {
159- Id = x,
160- Name = $"name {x}",
161- LastName = $"Surname {x}",
162- HireDate = DateTime.Now.Date.AddDays(-x)
163- }).ToList();
164- }
165-
166- protected async Task ReadItems(GridReadEventArgs args)
167- {
168- Console.WriteLine("before");
169- await Task.Delay(500); //delay for creating the GIF
170- Console.WriteLine("after");
171-
172- args.Data = SourceData.Skip(args.Request.Skip).Take(args.Request.PageSize).ToList();
173- args.Total = SourceData.Count;
174- }
175-
176- public class SampleData
177- {
178- public int Id { get; set; }
179- public string Name { get; set; }
180- public string LastName { get; set; }
181- public DateTime HireDate { get; set; }
182- }
183- }
184- -->
185129
186130## See Also
187131
0 commit comments