|
| 1 | +--- |
| 2 | +title: Aggregates |
| 3 | +page_title: Grid for Blazor | Aggregates |
| 4 | +description: Enable and configure field aggregates in Grid for Blazor |
| 5 | +slug: grid-aggregates |
| 6 | +tags: telerik,blazor,grid,aggreagates,aggregate |
| 7 | +published: True |
| 8 | +position: 23 |
| 9 | +--- |
| 10 | + |
| 11 | +# Grid Aggregates |
| 12 | + |
| 13 | +The Grid component provides built-in aggregates for column values based on [grouping]({%slug components/grid/features/grouping%}). |
| 14 | + |
| 15 | +There are several available aggregate functions under the `Telerik.Blazor.GridAggregateType` enum: |
| 16 | + |
| 17 | +* `Average` |
| 18 | +* `Count` |
| 19 | +* `Max` |
| 20 | +* `Min` |
| 21 | +* `Sum` |
| 22 | + |
| 23 | +To enable aggregates: |
| 24 | + |
| 25 | +1. Set the grid's `Groupable` property to `true`. |
| 26 | +1. Under the `GridAggregates` tag, define the `GridAggregate` entries to enable the aggregations per field you want to use. |
| 27 | +1. Use the aggregate result in the templates that support it by casting their `context` to a `GridGroupAggregateResult`: |
| 28 | + |
| 29 | + * `GroupFooterTemplate` - a footer in the respective column that renders when the grid is grouped. |
| 30 | + |
| 31 | +You should define only aggregates that you will use to avoid unnecessary calculations that may be noticeable on large data sets. |
| 32 | + |
| 33 | + |
| 34 | +>caption Use Aggregates in the Telerik Blazor Grid |
| 35 | +
|
| 36 | +````CSHTML |
| 37 | +@* Enable and use aggregates. To see the effect, group by a column, e.g., Team or "On Vacation" *@ |
| 38 | +
|
| 39 | +<TelerikGrid Data=@GridData Groupable="true" Pageable="true" Height="400px"> |
| 40 | + <GridAggregates> |
| 41 | + <GridAggregate Field=@nameof(Employee.Team) Aggregate="@GridAggregateType.Count" /> |
| 42 | + <GridAggregate Field=@nameof(Employee.Salary) Aggregate="@GridAggregateType.Max" /> |
| 43 | + <GridAggregate Field=@nameof(Employee.Salary) Aggregate="@GridAggregateType.Sum" /> |
| 44 | + <GridAggregate Field=@nameof(Employee.HireDate) Aggregate="@GridAggregateType.Min" /> |
| 45 | + </GridAggregates> |
| 46 | + <GridColumns> |
| 47 | + <GridColumn Field=@nameof(Employee.Name) Groupable="false" /> |
| 48 | + <GridColumn Field=@nameof(Employee.Team) Title="Team"> |
| 49 | + <GroupFooter> |
| 50 | + <strong>@((context as GridGroupAggregateResult).Sum)</strong> |
| 51 | + </GroupFooter> |
| 52 | + </GridColumn> |
| 53 | + <GridColumn Field=@nameof(Employee.Salary) Title="Salary" Groupable="false" /> |
| 54 | + <GridColumn Field=@nameof(Employee.HireDate) Title="Hire Date" Groupable="false" /> |
| 55 | + <GridColumn Field=@nameof(Employee.IsOnLeave) Title="On Vacation" /> |
| 56 | + </GridColumns> |
| 57 | +</TelerikGrid> |
| 58 | +
|
| 59 | +@code { |
| 60 | + public List<Employee> GridData { get; set; } |
| 61 | +
|
| 62 | + protected override void OnInitialized() |
| 63 | + { |
| 64 | + GridData = new List<Employee>(); |
| 65 | + var rand = new Random(); |
| 66 | + for (int i = 0; i < 15; i++) |
| 67 | + { |
| 68 | + Random rnd = new Random(); |
| 69 | + GridData.Add(new Employee() |
| 70 | + { |
| 71 | + EmployeeId = i, |
| 72 | + Name = "Employee " + i.ToString(), |
| 73 | + Team = "Team " + i % 3, |
| 74 | + Salary = rnd.Next(1000, 5000), |
| 75 | + IsOnLeave = i % 2 == 0 |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | + public class Employee |
| 81 | + { |
| 82 | + public int EmployeeId { get; set; } |
| 83 | + public string Name { get; set; } |
| 84 | + public string Team { get; set; } |
| 85 | + public decimal Salary { get; set; } |
| 86 | + public DateTime HireDate { get; set; } |
| 87 | + public bool IsOnLeave { get; set; } |
| 88 | + } |
| 89 | +} |
| 90 | +```` |
| 91 | + |
| 92 | + |
| 93 | +## See Also |
| 94 | + |
| 95 | + * [Live Demo: Grid Grouping](https://demos.telerik.com/blazor-ui/grid/grouping) |
| 96 | + |
| 97 | + |
0 commit comments