@@ -27,58 +27,60 @@ To enable virtual scrolling:
27
27
> caption Sample of virtual scrolling in the Telerik Grid for Blazor
28
28
29
29
```` CSHTML
30
- @* Scroll the grid instead of paging *@
30
+ @using Telerik.DataSource
31
+ @using Telerik.DataSource.Extensions
31
32
32
- <TelerikGrid Data=@GridData
33
+ <TelerikGrid OnRead="@OnGridRead"
34
+ TItem="@Product"
33
35
ScrollMode="@GridScrollMode.Virtual"
34
36
Height="480px" RowHeight="60" PageSize="20"
35
- Sortable="true" FilterMode="@GridFilterMode.FilterMenu ">
37
+ Sortable="true" FilterMode="@GridFilterMode.FilterRow ">
36
38
<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)" />
45
41
</GridColumns>
46
42
</TelerikGrid>
47
43
48
44
@code {
49
- public List<SampleData > GridData { get; set; }
45
+ private List<Product > GridData { get; set; } = new List<Product>();
50
46
51
- protected override async Task OnInitializedAsync( )
47
+ private async Task OnGridRead(GridReadEventArgs args )
52
48
{
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;
54
56
}
55
57
56
- private async Task<List<SampleData>> GetData ()
58
+ protected override void OnInitialized ()
57
59
{
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++)
59
64
{
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
+ }
65
72
}
66
73
67
- public class SampleData
74
+ public class Product
68
75
{
69
76
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; }
73
79
}
74
80
}
75
81
````
76
82
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.
82
84
83
85
## Notes
84
86
@@ -124,64 +126,6 @@ List of the known limitations of the virtual scrolling feature:
124
126
125
127
* 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.
126
128
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
- -->
185
129
186
130
## See Also
187
131
0 commit comments