Aggregates
The Telerik UI Grid for ASP.NET Core component provides built-in aggregates for a grand total row and also column values based on grouping.
In this article:
Available Aggregate Functions
The Telerik UI DataSource used by the Grid exposes several built-in aggregate functions:
Average
Count
Max
Min
Sum
The Count
aggregate can be applied to any type of field. The other aggregates can only be applied to numerical fields (e.g., int
, decimal
, double
, etc.).
Where You Can Use Aggregates
Aggregates can be used for the entire grid via:
-
ClientFooterTemplate
of aGridColumn
- a grand total row of footers for the entire grid.
Or, to each individual group via:
-
ClientGroupFooterTemplate
of aGridColumn
- a footer in the respective column that renders when the grid is grouped. -
ClientGroupHeaderColumnTemplate
of aGridColumn
- a header in the respective column that renders when the grid is grouped by that column. Thevalue
field in the context carries the current group value.
How to Enable Aggregates
To enable aggregates:
- Inside the
Aggregates
option of the data source, define the field and aggregate function. - Use the aggregate result in the templates that support it - their
context
is strongly typed and carries the aggregate values in the respective fields. - Set the grid's
Groupable
property totrue
.- If you will be using only
FooterTemplate
s - grouping is not required.
- If you will be using only
-
Group the grid to see the effect on group-specific templates.
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>() .Name("grid") .Columns(columns => { columns.Bound(p => p.ProductName) .ClientGroupFooterTemplate("Product: #=count#"); columns.Bound(p => p.UnitPrice).Format("{0:C}") .ClientFooterTemplate("Total price: #=sum#"); columns.Bound(p => p.UnitsInStock) .ClientGroupHeaderColumnTemplate("Units In Stock: #= value # (Count: #= count#)") .ClientGroupFooterTemplate("<div>Min: #= min #</div><div>Max: #= max #</div>"); }) .Groupable() .Pageable() .DataSource(dataSource => dataSource .Ajax() .Aggregates(aggregates => { aggregates.Add(p => p.ProductName).Count(); aggregates.Add(p => p.UnitPrice).Sum(); aggregates.Add(p => p.UnitsInStock).Min().Max().Count(); }) .Read(read => read.Action("AllProducts", "Grid")) ) )
Notes
- You should define only aggregates that you will use to avoid unnecessary calculations that may be noticeable on large data sets.
- If you try to use an aggregate that is not defined, or an aggregate over an unsupported field type, a JavaScript exception will be thrown.