Skip to content

Commit ca9d3d7

Browse files
docs(grid): example of getting data state from DataSourceRequest
1 parent a5c2294 commit ca9d3d7

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

components/grid/manual-operations.md

+85-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ The comments in the code provide explanations on what is done and why.
2424

2525
>tip You can also use a synchronous version of the event. Its signature is `void ReadItems(GridReadEventArgs args)`.
2626
27-
>caption Custom paging with a remote service
27+
Examples:
28+
29+
30+
* [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)
33+
* [Use OData Service](https://github.com/telerik/blazor-ui/tree/master/grid/odata)
34+
35+
### Custom paging with a remote service
2836

2937
````CSHTML
3038
Custom paging. 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.
@@ -110,7 +118,7 @@ Custom paging. There is a deliberate delay in the data source operations in this
110118
}
111119
````
112120

113-
>caption If you have all the data at once, the Telerik .ToDataSourceResult(request) extension method can manage the operations for you
121+
### If you have all the data at once, the Telerik .ToDataSourceResult(request) extension method can manage the operations for you
114122

115123
````CSHTML
116124
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.
@@ -193,6 +201,81 @@ Using Telerik DataSource extension methods to manipulate all the data into paged
193201
````
194202

195203

204+
### Extract information from the DataSourceRequest object to use in your own API
205+
206+
````CSHTML
207+
@using Telerik.DataSource
208+
@using Telerik.DataSource.Extensions
209+
210+
@ConsoleSim
211+
212+
<br />
213+
214+
<TelerikGrid Data=@CurrPageData OnRead="@OnReadHandler" TotalCount=@Total
215+
Sortable="true" FilterMode="@GridFilterMode.FilterRow"
216+
Pageable="true" PageSize="15"
217+
Height="400px">
218+
<GridColumns>
219+
<GridColumn Field="Id" />
220+
<GridColumn Field="Name" />
221+
<GridColumn Field="Team" />
222+
<GridColumn Field="HireDate" />
223+
</GridColumns>
224+
</TelerikGrid>
225+
226+
227+
@functions {
228+
MarkupString ConsoleSim { get; set; }// to showcase what you get
229+
230+
//implementation of OnRead
231+
List<SampleData> CurrPageData { get; set; }
232+
int Total { get; set; }
233+
234+
async Task OnReadHandler(GridReadEventArgs args)
235+
{
236+
string output = string.Empty;
237+
output += "FILTERS:<br />";
238+
//loop the DataSourceRequest collections to extract the data you require
239+
foreach (FilterDescriptor item in args.Request.Filters)
240+
{
241+
output += $"field: {item.Member}, operator {item.Operator}, value: {item.Value}<br />";
242+
}
243+
output += "SORTS:<br />";
244+
foreach (SortDescriptor item in args.Request.Sorts)
245+
{
246+
output += $"field: {item.Member}, direction: {item.SortDirection} <br />";
247+
}
248+
output += $"Current page: {args.Request.Page}, page size: {args.Request.PageSize}";
249+
250+
//show that data in the UI for a visual aid
251+
ConsoleSim = new MarkupString(output);
252+
253+
//actual data source operation, implement as required in your case (e.g., call a service with parameters you built)
254+
var result = PristineData.ToDataSourceResult(args.Request);
255+
CurrPageData = (result.Data as IEnumerable<SampleData>).ToList();
256+
Total = result.Total;
257+
258+
StateHasChanged();
259+
}
260+
261+
public IEnumerable<SampleData> PristineData = Enumerable.Range(1, 300).Select(x => new SampleData
262+
{
263+
Id = x,
264+
Name = "name " + x,
265+
Team = "team " + x % 5,
266+
HireDate = DateTime.Now.AddDays(-x).Date
267+
});
268+
269+
public class SampleData
270+
{
271+
public int Id { get; set; }
272+
public string Name { get; set; }
273+
public string Team { get; set; }
274+
public DateTime HireDate { get; set; }
275+
}
276+
}
277+
````
278+
196279

197280
## See Also
198281

0 commit comments

Comments
 (0)