From b122c59f5ab5f3d32902d51c9deba1aac0b22b75 Mon Sep 17 00:00:00 2001 From: Nadezhda Tacheva <73842592+ntacheva@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:25:54 +0300 Subject: [PATCH] chore(grid): add null checks for the AggregateResults (#2331) * chore(grid): add null checks for the AggregateResults * Update aggregates.md --------- Co-authored-by: Dimo Dimov <961014+dimodi@users.noreply.github.com> --- components/grid/grouping/aggregates.md | 61 ++++++++++++++++---------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/components/grid/grouping/aggregates.md b/components/grid/grouping/aggregates.md index 19860be5f..1c5e5a73b 100644 --- a/components/grid/grouping/aggregates.md +++ b/components/grid/grouping/aggregates.md @@ -64,7 +64,11 @@ To enable aggregates: >caption Use Aggregates in the Telerik Blazor Grid ````CSHTML - +@using Telerik.DataSource + + @@ -76,33 +80,36 @@ To enable aggregates: - Total: @context.Count employees + Total employees: @context.Count
@{ // you can use aggregates for other fields/columns by extracting the desired one by its // field name and aggregate function from the AggregateResults collection // The type of its Value is determined by the type of its field - decimal for the Salary field here - decimal salaries = (decimal)context.AggregateResults + decimal? salaries = (decimal?)context.AggregateResults .FirstOrDefault(r => r.AggregateMethodName == nameof(GridAggregateType.Sum) && r.Member == nameof(Employee.Salary))?.Value; + + Total salaries: @salaries?.ToString("C0") } - Total salaries: @salaries.ToString("C0")
- @context.Value @* the default text you would get without the template *@ - Team size: @context.Count + + @context.Value @* the default text you would get without the template *@ + with employee count: @context.Count + Team Members: @context.Count - + @* you can use a group footer for non-groupable columns as well *@ - Total salaries: @context.Sum + Total salaries: @context.Sum?.ToString("C0")
- Highest: @context.Max + Highest: @context.Max?.ToString("C0")
@@ -119,14 +126,15 @@ To enable aggregates: @*access the aggregates of the ActiveProjects column*@ - All active projects: @context.Sum + Active projects in team: @context.Sum + + @* access the aggregates of the other columns if any *@
- @*access the aggregates of the other columns*@ - Total teams: @context.AggregateResults[nameof(Employee.Team)].Count + Total teams: @context.AggregateResults[nameof(Employee.Team)]?.Count
- Total employees: @context.AggregateResults[nameof(Employee.Name)].Count + Total employees: @context.AggregateResults[nameof(Employee.Name)]?.Count
- Average salary: @context.AggregateResults[nameof(Employee.Salary)].Average.Value.ToString("C0") + Average salary: @context.AggregateResults[nameof(Employee.Salary)]?.Average?.ToString("C0")
@@ -135,19 +143,26 @@ To enable aggregates: @code { private List GridData { get; set; } = new(); + private void OnGridStateInit(GridStateEventArgs args) + { + args.GridState.GroupDescriptors.Add(new GroupDescriptor() + { + Member = nameof(Employee.Team) + }); + } + protected override void OnInitialized() { - for (int i = 0; i < 15; i++) + for (int i = 1; i <= 5; i++) { - Random rnd = new Random(); GridData.Add(new Employee() - { - EmployeeId = i, - Name = "Employee " + i.ToString(), - Team = "Team " + i % 3, - Salary = rnd.Next(1000, 5000), - ActiveProjects = i % 4 == 0 ? 2 : 5 - }); + { + EmployeeId = i, + Name = $"Employee {i}", + Team = $"Team {i % 2 + 1}", + Salary = Random.Shared.Next(1000, 5000), + ActiveProjects = i % 4 == 0 ? 2 : 5 + }); } }