Skip to content

Commit 636378f

Browse files
docs(grid): cache data source request
1 parent 4c478ef commit 636378f

File tree

1 file changed

+121
-4
lines changed

1 file changed

+121
-4
lines changed

components/grid/manual-operations.md

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ Examples:
2828

2929

3030
* [Custom paging with a remote service](#custom-paging-with-a-remote-service)
31-
* [If you have all the data at once, the Telerik .ToDataSourceResult(request) extension method can manage the operations for you](#if-you-have-all-the-data-at-once-the-telerik-todatasourceresultrequest-extension-method-can-manage-the-operations-for-you)
32-
* [Extract information from the DataSourceRequest object to use in your own API](#extract-information-from-the-datasourcerequest-object-to-use-in-your-own-api)
31+
* [Telerik .ToDataSourceResult(request)](#telerik-todatasourceresultrequest)
32+
* [Get Information From the DataSourceRequest](#get-information-from-the-datasourcerequest)
33+
* [Cache Data Request](#cache-data-request)
3334
* [Use OData Service](https://github.com/telerik/blazor-ui/tree/master/grid/odata)
3435

3536
### Custom paging with a remote service
@@ -118,7 +119,9 @@ Custom paging. There is a deliberate delay in the data source operations in this
118119
}
119120
````
120121

121-
### If you have all the data at once, the Telerik .ToDataSourceResult(request) extension method can manage the operations for you
122+
### Telerik .ToDataSourceResult(request)
123+
124+
If you have all the data at once, the Telerik .ToDataSourceResult(request) extension method can manage the operations for you
122125

123126
````CSHTML
124127
Using Telerik DataSource extension methods to manipulate all the data into paged chunks and also perform other operations like filtering, sorting, etc. There is a deliberate delay in the data source operations in this example to mimic real life delays and to showcase the async nature of the calls.
@@ -201,7 +204,9 @@ Using Telerik DataSource extension methods to manipulate all the data into paged
201204
````
202205

203206

204-
### Extract information from the DataSourceRequest object to use in your own API
207+
### Get Information From the DataSourceRequest
208+
209+
With a few simple loops, you can extract information from the DataSourceRequest object to use in your own API (such as filters, sorts, paging state).
205210

206211
````CSHTML
207212
@using Telerik.DataSource
@@ -277,6 +282,118 @@ Using Telerik DataSource extension methods to manipulate all the data into paged
277282
````
278283

279284

285+
### Cache Data Request
286+
287+
If you need to replay the last request for some reason (your data has updated, or you need to await some business logic that determines what data to request), store the `DataSourceRequest` object in a field in your view model, then run the method that will read the data when necessary - a button click, or when some async operation completes.
288+
289+
290+
````CSHTML
291+
@* This example awaits some business data in OnInitializedAsync and fetches grid data according to it
292+
You can call the SetGridData() method from button clicks or other events according to your needs *@
293+
294+
@using Telerik.DataSource.Extensions
295+
@using Telerik.DataSource
296+
297+
<TelerikGrid Data=@GridData TotalCount=@Total OnRead=@ReadItems
298+
[email protected] Sortable=true Pageable=true>
299+
<GridColumns>
300+
<GridColumn Field=@nameof(Employee.ID) />
301+
<GridColumn Field=@nameof(Employee.Name) Title="Name" />
302+
<GridColumn Field=@nameof(Employee.HireDate) Title="Hire Date" />
303+
</GridColumns>
304+
</TelerikGrid>
305+
306+
@code {
307+
string something { get; set; } // an object on which the grid data depends
308+
309+
protected override async Task OnInitializedAsync()
310+
{
311+
// the business logic that determines the global object - such as a user, their role, access rights, settings, etc.
312+
await GetSomething();
313+
314+
// Refresh the Grid Data after the business data has arrived
315+
SetGridData();
316+
317+
await base.OnInitializedAsync();
318+
}
319+
320+
async Task GetSomething()
321+
{
322+
// in a real case - apply the desired business logic here
323+
await Task.Delay(3000);
324+
something = DateTime.Now.Millisecond.ToString();
325+
}
326+
327+
public List<Employee> SourceData { get; set; }
328+
public List<Employee> GridData { get; set; }
329+
public int Total { get; set; } = 0;
330+
331+
// cache the last data request so you can "replay" it and update the grid data anytime
332+
public DataSourceRequest CurrentRequest { get; set; }
333+
334+
335+
protected async Task ReadItems(GridReadEventArgs args)
336+
{
337+
// cache the last data request so you can update teh grid data with it at any time
338+
CurrentRequest = args.Request;
339+
340+
if (!string.IsNullOrEmpty(something)) // business logic that dictates when/what to request for the grid
341+
{
342+
SetGridData();
343+
}
344+
}
345+
346+
// Update the grid data with the cached request at any time
347+
private void SetGridData()
348+
{
349+
if (CurrentRequest == null)
350+
{
351+
return;
352+
}
353+
354+
// implement actual reading of the data, for example, use the "something" business object above
355+
// this is merely some generated data to get the grid running
356+
var datasourceResult = SourceData.ToDataSourceResult(CurrentRequest);
357+
358+
GridData = (datasourceResult.Data as IEnumerable<Employee>).ToList();
359+
Total = datasourceResult.Total;
360+
}
361+
362+
//This sample implements only reading of the data. To add the rest of the CRUD operations see
363+
//https://docs.telerik.com/blazor-ui/components/grid/editing/overview
364+
365+
protected override void OnInitialized()
366+
{
367+
SourceData = GenerateData();
368+
}
369+
370+
private List<Employee> GenerateData()
371+
{
372+
var result = new List<Employee>();
373+
var rand = new Random();
374+
for (int i = 0; i < 100; i++)
375+
{
376+
result.Add(new Employee()
377+
{
378+
ID = i,
379+
Name = "Name " + i,
380+
HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20))
381+
});
382+
}
383+
384+
return result;
385+
}
386+
387+
public class Employee
388+
{
389+
public int ID { get; set; }
390+
public string Name { get; set; }
391+
public DateTime HireDate { get; set; }
392+
}
393+
}
394+
````
395+
396+
280397
## See Also
281398

282399
* [CRUD Operations Overview]({%slug components/grid/editing/overview%})

0 commit comments

Comments
 (0)