Completed
Last Updated: 23 Oct 2020 13:04 by ADMIN
Release 2.19.0
Jan Hindrik
Created on: 07 Jun 2020 17:37
Category: Grid
Type: Bug Report
10
Aggregates don't work when grouping is set in OnStateInit

If I set grouping in OnStateInit, the GroupFooterTemplate does not have data for the aggregates I have set. If I group manually by dragging a column header, the data is there.

------

ADMIN EDIT: This stems from a framework behavior. The <GridAggregates> tag is a child component of the grid and as such, initializes after the grid. Since there is no event when all such child components are initialized the parent component cannot wait for them before starting to render and thus, it initializes before it can know what aggregates are defined.

1 comment
ADMIN
Marin Bratanov
Posted on: 07 Jun 2020 17:40

A workaround can be using OnAfterRender to set the grid state (downside: will cause a second data request when using OnRead). Here's an example of the workaround:

@using Telerik.DataSource;

<TelerikGrid Data=@GridData Groupable="true" Pageable="true" Height="650px"
             @ref="@GridRef">
    <GridAggregates>
        <GridAggregate Field=@nameof(Employee.Period1) Aggregate="@GridAggregateType.Sum" />
        <GridAggregate Field=@nameof(Employee.Period2) Aggregate="@GridAggregateType.Sum" />
        <GridAggregate Field=@nameof(Employee.Period3) Aggregate="@GridAggregateType.Sum" />
    </GridAggregates>
    <GridColumns>
        <GridColumn Field=@nameof(Employee.Name) Groupable="false" />
        <GridColumn Field=@nameof(Employee.Team) Title="Team">
        </GridColumn>
        <GridColumn Field=@nameof(Employee.Salary) Title="Salary" Groupable="false">
        </GridColumn>
        <GridColumn Field=@nameof(Employee.ActiveProjects) Title="Active Projects">
        </GridColumn>
        <GridColumn Field=@nameof(Employee.Period1) Title="Period 1">
            <GroupFooterTemplate>
                @context.Sum
            </GroupFooterTemplate>
        </GridColumn>
        <GridColumn Field=@nameof(Employee.Period2) Title="Period 2">
            <GroupFooterTemplate>
                @context.Sum
            </GroupFooterTemplate>
        </GridColumn>
        <GridColumn Field=@nameof(Employee.Period3) Title="Period 3">
            <GroupFooterTemplate>
                @context.Sum
            </GroupFooterTemplate>
        </GridColumn>
    </GridColumns>
</TelerikGrid>

@code {
        TelerikGrid<Employee> GridRef { get; set; }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            GridState<Employee> desiredState = new GridState<Employee>()
            {
                GroupDescriptors = new List<GroupDescriptor>()
            {
                new GroupDescriptor()
                {
                    Member = "Team",
                    MemberType = typeof(string)
                }
            }
            };
            await GridRef.SetState(desiredState);
        }
    }


    public List<Employee> GridData { get; set; }

    protected override void OnInitialized()
    {
        GridData = new List<Employee>();
        var rand = new Random();
        for (int i = 0; i < 15; 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,
                Period1 = i,
                Period2 = i * i,
                Period3 = i ^ 2
            });
        }
    }

    public class Employee
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Team { get; set; }
        public decimal Salary { get; set; }
        public int ActiveProjects { get; set; }
        public decimal Period1 { get; set; }
        public decimal Period2 { get; set; }
        public decimal Period3 { get; set; }
    }
}

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.