@@ -64,7 +64,11 @@ To enable aggregates:
64
64
> caption Use Aggregates in the Telerik Blazor Grid
65
65
66
66
```` CSHTML
67
- <TelerikGrid Data=@GridData Groupable="true" Height="700px">
67
+ @using Telerik.DataSource
68
+
69
+ <TelerikGrid Data=@GridData
70
+ Groupable="true"
71
+ OnStateInit="@( (GridStateEventArgs<Employee> args) => OnGridStateInit(args) )">
68
72
<GridAggregates>
69
73
<GridAggregate Field=@nameof(Employee.Name) Aggregate="@GridAggregateType.Count" />
70
74
<GridAggregate Field=@nameof(Employee.Team) Aggregate="@GridAggregateType.Count" />
@@ -76,33 +80,36 @@ To enable aggregates:
76
80
<GridColumns>
77
81
<GridColumn Field=@nameof(Employee.Name) Groupable="false">
78
82
<FooterTemplate>
79
- Total: @context.Count employees
83
+ Total employees : @context.Count
80
84
<br />
81
85
@{
82
86
// you can use aggregates for other fields/columns by extracting the desired one by its
83
87
// field name and aggregate function from the AggregateResults collection
84
88
// The type of its Value is determined by the type of its field - decimal for the Salary field here
85
- decimal salaries = (decimal)context.AggregateResults
89
+ decimal? salaries = (decimal? )context.AggregateResults
86
90
.FirstOrDefault(r => r.AggregateMethodName == nameof(GridAggregateType.Sum) && r.Member == nameof(Employee.Salary))?.Value;
91
+
92
+ <span>Total salaries: @salaries?.ToString("C0")</span>
87
93
}
88
- Total salaries: @salaries.ToString("C0")
89
94
</FooterTemplate>
90
95
</GridColumn>
91
96
<GridColumn Field=@nameof(Employee.Team) Title="Team">
92
97
<GroupHeaderTemplate>
93
- @context.Value @* the default text you would get without the template *@
94
- <span>Team size: @context.Count</span>
98
+ <span>
99
+ @context.Value @* the default text you would get without the template *@
100
+ with employee count: @context.Count
101
+ </span>
95
102
</GroupHeaderTemplate>
96
103
<GroupFooterTemplate>
97
104
Team Members: <strong>@context.Count</strong>
98
105
</GroupFooterTemplate>
99
106
</GridColumn>
100
- <GridColumn Field=@nameof(Employee.Salary) Title="Salary" Groupable="false">
107
+ <GridColumn Field=@nameof(Employee.Salary) Title="Salary" Groupable="false" DisplayFormat="{0:C0}" >
101
108
<GroupFooterTemplate>
102
109
@* you can use a group footer for non-groupable columns as well *@
103
- Total salaries: @context.Sum
110
+ Total salaries: @context.Sum?.ToString("C0")
104
111
<br />
105
- <span style="color: red;">Highest: @context.Max</span>
112
+ <span style="color: red;">Highest: @context.Max?.ToString("C0") </span>
106
113
</GroupFooterTemplate>
107
114
</GridColumn>
108
115
<GridColumn Field=@nameof(Employee.ActiveProjects) Title="Active Projects">
@@ -119,14 +126,15 @@ To enable aggregates:
119
126
</GroupHeaderTemplate>
120
127
<GroupFooterTemplate>
121
128
@*access the aggregates of the ActiveProjects column*@
122
- All active projects: @context.Sum
129
+ Active projects in team: @context.Sum
130
+
131
+ @* access the aggregates of the other columns if any *@
123
132
<br />
124
- @*access the aggregates of the other columns*@
125
- Total teams: @context.AggregateResults[nameof(Employee.Team)].Count
133
+ <span>Total teams: @context.AggregateResults[nameof(Employee.Team)]?.Count</span>
126
134
<br />
127
- Total employees: @context.AggregateResults[nameof(Employee.Name)].Count
135
+ <span> Total employees: @context.AggregateResults[nameof(Employee.Name)]? .Count</span>
128
136
<br />
129
- Average salary: @context.AggregateResults[nameof(Employee.Salary)].Average.Value. ToString("C0")
137
+ <span> Average salary: @context.AggregateResults[nameof(Employee.Salary)]? .Average?. ToString("C0")</span>
130
138
</GroupFooterTemplate>
131
139
</GridColumn>
132
140
</GridColumns>
@@ -135,19 +143,26 @@ To enable aggregates:
135
143
@code {
136
144
private List<Employee> GridData { get; set; } = new();
137
145
146
+ private void OnGridStateInit(GridStateEventArgs<Employee> args)
147
+ {
148
+ args.GridState.GroupDescriptors.Add(new GroupDescriptor()
149
+ {
150
+ Member = nameof(Employee.Team)
151
+ });
152
+ }
153
+
138
154
protected override void OnInitialized()
139
155
{
140
- for (int i = 0 ; i < 15 ; i++)
156
+ for (int i = 1 ; i <= 5 ; i++)
141
157
{
142
- Random rnd = new Random();
143
158
GridData.Add(new Employee()
144
- {
145
- EmployeeId = i,
146
- Name = "Employee " + i.ToString() ,
147
- Team = "Team " + i % 3 ,
148
- Salary = rnd .Next(1000, 5000),
149
- ActiveProjects = i % 4 == 0 ? 2 : 5
150
- });
159
+ {
160
+ EmployeeId = i,
161
+ Name = $ "Employee {i}" ,
162
+ Team = $ "Team { i % 2 + 1}" ,
163
+ Salary = Random.Shared .Next(1000, 5000),
164
+ ActiveProjects = i % 4 == 0 ? 2 : 5
165
+ });
151
166
}
152
167
}
153
168
0 commit comments