Skip to content

Commit ce49f09

Browse files
docs(grid): aggregates (wip)
1 parent fc294f0 commit ce49f09

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

components/grid/aggregates.md

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,68 @@ There are several available aggregate functions under the `Telerik.Blazor.GridAg
2020
* `Min`
2121
* `Sum`
2222

23+
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.).
24+
25+
You can use aggregates in the following templates:
26+
27+
* `GroupFooterTemplate` of a `GridColumn` - a footer in the respective column that renders when the grid is grouped.
28+
* `GroupHeaderTemplate` of a `GridColumn` - a header in the respective column that renders when the grid is grouped by that column. The `Value` field in the context carries the current group value.
29+
2330
To enable aggregates:
2431

2532
1. Set the grid's `Groupable` property to `true`.
2633
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.
34+
1. Use the aggregate result in the templates that support it - their `context` is strongly typed and carries the aggregate values in the respective fields.
35+
1. Group the grid to see the effect
3036

3137
You should define only aggregates that you will use to avoid unnecessary calculations that may be noticeable on large data sets.
3238

39+
If you try to use an aggregate that is not defined, or an aggregate over an unsupported field type, a runtime error will be thrown.
40+
3341

3442
>caption Use Aggregates in the Telerik Blazor Grid
3543
3644
````CSHTML
37-
@* Enable and use aggregates. To see the effect, group by a column, e.g., Team or "On Vacation" *@
45+
@* Enable and use aggregates. To see the effect, group by a column - "Team" and then "Active Projects" *@
3846
39-
<TelerikGrid Data=@GridData Groupable="true" Pageable="true" Height="400px">
47+
<TelerikGrid Data=@GridData Groupable="true" Pageable="true" Height="650px">
4048
<GridAggregates>
4149
<GridAggregate Field=@nameof(Employee.Team) Aggregate="@GridAggregateType.Count" />
4250
<GridAggregate Field=@nameof(Employee.Salary) Aggregate="@GridAggregateType.Max" />
4351
<GridAggregate Field=@nameof(Employee.Salary) Aggregate="@GridAggregateType.Sum" />
44-
<GridAggregate Field=@nameof(Employee.HireDate) Aggregate="@GridAggregateType.Min" />
4552
</GridAggregates>
4653
<GridColumns>
4754
<GridColumn Field=@nameof(Employee.Name) Groupable="false" />
4855
<GridColumn Field=@nameof(Employee.Team) Title="Team">
49-
<GroupFooter>
50-
<strong>@((context as GridGroupAggregateResult).Sum)</strong>
51-
</GroupFooter>
56+
<GroupFooterTemplate>
57+
Team Members: <strong>@context.Count</strong>
58+
</GroupFooterTemplate>
59+
<GroupHeaderTemplate>
60+
@context.Value @* the default text you would get without the template *@
61+
&nbsp;<span>Team size: @context.Count</span>
62+
</GroupHeaderTemplate>
63+
</GridColumn>
64+
<GridColumn Field=@nameof(Employee.Salary) Title="Salary" Groupable="false">
65+
<GroupFooterTemplate>
66+
@* you can use a group footer for non-groupable columns as well *@
67+
Total montly salary: @context.Sum
68+
<br />
69+
<span style="color: red;">Top paid employee: @context.Max</span>
70+
</GroupFooterTemplate>
71+
</GridColumn>
72+
<GridColumn Field=@nameof(Employee.ActiveProjects) Title="Active Projects">
73+
<GroupHeaderTemplate>
74+
@{
75+
<span>Currently active projects: @context.Value &nbsp;</span>
76+
77+
//sample of conditional logic in the group header
78+
if ( (int)context.Value > 3) // in a real case, you may want to ensure type safety and add defensive checks
79+
{
80+
<strong style="color: red;">These people work on too many projects</strong>
81+
}
82+
}
83+
</GroupHeaderTemplate>
5284
</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" />
5685
</GridColumns>
5786
</TelerikGrid>
5887
@@ -72,7 +101,7 @@ You should define only aggregates that you will use to avoid unnecessary calcula
72101
Name = "Employee " + i.ToString(),
73102
Team = "Team " + i % 3,
74103
Salary = rnd.Next(1000, 5000),
75-
IsOnLeave = i % 2 == 0
104+
ActiveProjects = i % 4 == 0 ? 2 : 5
76105
});
77106
}
78107
}
@@ -83,12 +112,15 @@ You should define only aggregates that you will use to avoid unnecessary calcula
83112
public string Name { get; set; }
84113
public string Team { get; set; }
85114
public decimal Salary { get; set; }
86-
public DateTime HireDate { get; set; }
87-
public bool IsOnLeave { get; set; }
115+
public int ActiveProjects { get; set; }
88116
}
89117
}
90118
````
91119

120+
>caption The result of the code snippet above after the grid has been grouped by the `Team` and `Active Projects` columns
121+
122+
![](images/grid-aggregates-overview.png)
123+
92124

93125
## See Also
94126

components/grid/grouping.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ To remove a group setting, click the `[x]` button on its indicator in the group
2222

2323
To prevent grouping by a field, set `Groupable="false"` for on its column. This can be useful for fields with unique values like IDs or names.
2424

25+
You can also use [aggregates]({%slug grid-aggregates%}) for the grouped data.
26+
2527
>caption Enable grouping in Telerik Grid
2628
2729
````CSHTML
@@ -72,5 +74,6 @@ Drag the column header of the "Team" and/or "On Vacation" column to the group pa
7274
## See Also
7375

7476
* [Live Demo: Grid Grouping](https://demos.telerik.com/blazor-ui/grid/grouping)
77+
* [Grid Aggregates]({%slug grid-aggregates%})
7578

7679

55.5 KB
Loading

components/grid/templates.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ The Grid component can use templates for:
1515
* [rows](#row-template)
1616
* [editing of a field](#edit-template)
1717
* [column header](#header-template)
18+
* [column group footer](#column-group-footer)
19+
* [group header](#group-header)
20+
1821

1922
Like other Blazor content, they can receive a `context` argument that is the type of the model. To use templates, you must bind the grid to a named model.
2023

@@ -304,6 +307,14 @@ Bound columns render the name of the field or their `Title` in their header. Thr
304307

305308
>note Header Templates are not available for the `GridCheckboxColumn` and the `GridCommandColumn`.
306309
310+
## Column Group Footer
311+
312+
When the grid is grouped, the columns can display a footer with information about the column data and some custom text/logic. The template is strongly typed and exposes the available aggregates values. For more information and examples, see the [Aggregates]({%slug grid-aggregates%}) article.
313+
314+
## Group Header
315+
316+
When the grid is grouped, the top row above the group provides information about the current group value by default. You can use this template to add custom content there in addition to the current value. For more information and examples, see the [Aggregates]({%slug grid-aggregates%}) article.
317+
307318
## See Also
308319

309320
* [Live Demo: Grid Templates](https://demos.telerik.com/blazor-ui/grid/templates)

0 commit comments

Comments
 (0)