Skip to content

Commit 906b9a3

Browse files
authored
docs(grid): Add info about aggregates and OnRead (#1082)
1 parent e267f9e commit 906b9a3

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

components/grid/grouping/aggregates.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ The Grid component provides built-in aggregates for column values based on [grou
1515

1616
#### In this article:
1717

18-
1918
* [Available Aggregate Functions](#available-aggregate-functions)
2019
* [Where You Can Use Aggregates](#where-you-can-use-aggregates)
2120
* [How to Enable Aggregates](#how-to-enable-aggregates)
@@ -153,7 +152,6 @@ Enable and use aggregates. To see the full effect, group by a column - "Team" an
153152
![Blazor Grid Aggregates Overview](images/grid-aggregates-overview.png)
154153

155154

156-
157155
## Notes
158156

159157
* You should define only aggregates that you will use to avoid unnecessary calculations that may be noticeable on large data sets.
@@ -162,11 +160,22 @@ Enable and use aggregates. To see the full effect, group by a column - "Team" an
162160

163161
* If you update a field of a model the `Data` collection in the view-model, aggregates will not be updated automatically - the grid needs to re-evaluate that data first, and since this is an expensive operation a UI render does not trigger it. You can [update the data collection]({%slug grid-refresh-data%}) yourself, or fetching it anew from the service (example [here]({%slug components/grid/editing/overview%}), see how the Create/Update/Delete events fetch data anew).
164162

165-
* If you use [`OnRead`]({%slug components/grid/manual-operations%}), the Grid will calculate aggregates from the data on the current page only. It is still possible to [calculate and display aggregates, which are based on all the data]({%slug grid-templates-column-footer%}#notes).
163+
* If you [bind the Grid via `OnRead` event]({%slug components/grid/manual-operations%}), make sure to set `AggregateResults` in the `GridReadEventArgs` event argument object. Otherwise the Grid will calculate aggregates from the data on the current page only.
164+
165+
<div class="skip-repl"></div>
166+
167+
```CS
168+
private async Task OnGridRead(GridReadEventArgs args)
169+
{
170+
DataSourceResult result = AllGridData.ToDataSourceResult(args.Request);
171+
172+
args.Data = result.Data;
173+
args.Total = result.Total;
174+
args.AggregateResults = result.AggregateResults;
175+
}
176+
```
166177

167178

168179
## See Also
169180

170-
* [Live Demo: Grid Grouping](https://demos.telerik.com/blazor-ui/grid/grouping)
171-
172-
181+
* [Live Demo: Grid Grouping](https://demos.telerik.com/blazor-ui/grid/grouping)

components/grid/manual-operations.md

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,12 @@ position: 55
1010

1111
# Manual Data Source Operations
1212

13-
By default, the grid will receive the entire collection of data, and it will perform the necessary operations (like [paging]({%slug components/grid/features/paging%}), [sorting]({%slug components/grid/features/sorting%}), [filtering]({%slug components/grid/filtering%})) internally to it. You can perform these operations yourself by handling the `OnRead` event of the grid as shown in the example below. The data source will be read after each [CUD operation]({%slug components/grid/editing/overview%}) as well, to ensure fresh data.
13+
By default, the Grid will receive the entire collection of data, and it will perform the necessary operations (like [paging]({%slug components/grid/features/paging%}), [sorting]({%slug components/grid/features/sorting%}), [filtering]({%slug components/grid/filtering%})) internally.
1414

15-
>tip This article describes how you can load data on demand and implement filtering, paging, sorting on the server.
16-
>
17-
> Detailed examples showcase the functionality of the [`OnRead` event]({%slug common-features-data-binding-onread%}) that you need to use.
18-
>
19-
> You can also find runnable sample apps in the [Serialize the DataSoureRequest to the server](https://github.com/telerik/blazor-ui/tree/master/grid/datasourcerequest-on-server) repo.
20-
21-
The parameter of type `DataSourceRequest` exposes information about the desired paging, filtering and sorting so you can, for example, call your remote endpoint with appropriate parameters so its performance is optimized and it fetches only the relevant data.
15+
You can perform all data operations yourself (e.g. on the server) and load data on demand by using the `OnRead` event of the Grid. The data source will be read after each [CUD operation]({%slug components/grid/editing/overview%}) as well, to ensure fresh data.
2216

23-
When the `OnRead` event is used, the internal operations are disabled and you must perform them all in the `OnRead` event. You must set:
17+
>tip Make sure to get familiar with all the general [`OnRead` event documentation]({%slug common-features-data-binding-onread%}) first.
2418
25-
* the data to `args.Data`
26-
* the total number of items (if there is no paging) to `args.Total`
27-
* the Grid itself should set the `TItem` attribute to the model type
28-
29-
The event arguments of `OnRead` will receive the `Page` that comes from the application. For example, if you load the grid [state]({%slug grid-state%}), or you set the parameter value yourself, that value will be passed to the event handler. If it is out of the range of the available data, it will be up to the application to handle that discrepancy.
3019

3120
## Examples
3221

@@ -39,6 +28,7 @@ Examples:
3928
* [Custom paging with a remote service](#custom-paging-with-a-remote-service)
4029
* [Telerik .ToDataSourceResult(request)](#telerik-todatasourceresultrequest)
4130
* [Grouping with OnRead](#grouping-with-onread)
31+
* [Aggregates with OnRead](#aggregates-with-onread)
4232
* [Get Information From the DataSourceRequest](#get-information-from-the-datasourcerequest)
4333
* [Use OData Service](https://github.com/telerik/blazor-ui/tree/master/grid/odata)
4434
* [Serialize the DataSoureRequest to the server](https://github.com/telerik/blazor-ui/tree/master/grid/datasourcerequest-on-server)
@@ -256,6 +246,7 @@ This sample shows how to set up the grid to use grouping with manual data source
256246
args.Data = datasourceResult.Data.Cast<object>().ToList();
257247
258248
args.Total = datasourceResult.Total;
249+
args.AggregateResults = datasourceResult.AggregateResults;
259250
}
260251
261252
protected override void OnInitialized()
@@ -298,6 +289,24 @@ This sample shows how to set up the grid to use grouping with manual data source
298289
> Thus, clicking the built-in Add command button on its toolbar when there is no data will produce a `null` item and if you have editor templates, there may be null reference errors (the `context` will be `null`). To avoid that, you can [initiate insertion of items through the grid state]({%slug grid-state%}#initiate-editing-or-inserting-of-an-item) in order to ensure a model reference exists.
299290
300291

292+
## Aggregates with OnRead
293+
294+
When using [aggregates]({%slug grid-aggregates%}) with `OnRead`, the Grid expects you to set one more property of the `GridReadEventArgs` object - `AggregateResults`. Otherwise the component will show aggregate values for the current page only.
295+
296+
<div class="skip-repl"></div>
297+
298+
```CS
299+
private async Task OnGridRead(GridReadEventArgs args)
300+
{
301+
DataSourceResult result = AllGridData.ToDataSourceResult(args.Request);
302+
303+
args.Data = result.Data;
304+
args.Total = result.Total;
305+
args.AggregateResults = result.AggregateResults;
306+
}
307+
```
308+
309+
301310
## Get Information From the DataSourceRequest
302311

303312
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).
@@ -390,9 +399,9 @@ With a few simple loops, you can extract information from the DataSourceRequest
390399

391400
## See Also
392401

393-
* [CRUD Operations Overview]({%slug components/grid/editing/overview%})
394-
* [Live Demo: Manual Data Source Operations](https://demos.telerik.com/blazor-ui/grid/manual-operations)
395-
* [Use OData Service](https://github.com/telerik/blazor-ui/tree/master/grid/odata)
396-
* [Custom Server Operations](https://github.com/telerik/blazor-ui/tree/master/grid/datasourcerequest-on-server)
397-
* [DataSourceRequest Object API](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.DataSourceRequest)
398-
* [DataSourceResult Object API](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.DataSourceResult)
402+
* [CRUD Operations Overview]({%slug components/grid/editing/overview%})
403+
* [Live Demo: Manual Data Source Operations](https://demos.telerik.com/blazor-ui/grid/manual-operations)
404+
* [Use OData Service](https://github.com/telerik/blazor-ui/tree/master/grid/odata)
405+
* [Custom Server Operations](https://github.com/telerik/blazor-ui/tree/master/grid/datasourcerequest-on-server)
406+
* [DataSourceRequest Object API](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.DataSourceRequest)
407+
* [DataSourceResult Object API](https://docs.telerik.com/blazor-ui/api/Telerik.DataSource.DataSourceResult)

0 commit comments

Comments
 (0)