Get Parent Aggregate Count in Child Grouping

Hi there,

I would like to be able to calculate sub-group proportions

In the example below, there is a group by the Parent column and the Count is 8.

Next, there is a sub-group on the Child column, and the Counts are 7, and 1, respectively. 

In the sub-group caption, I want to show 

  • Sub Group 1 : 7 (87.5%) 
  • Sub Group 2 : 1 (12.5%) 

grouping.png

The problem is, when creating the caption at the sub-group level , the  CaptionTemplateContext does not contain any information which would enable me to calculate counts at the parent level


That is, I can't calculate 7/8 = 0.875  because I cannot find out that the parent count is 8.

Is there a way to solve this?




6 Replies

RN Rahul Narayanasamy Syncfusion Team March 28, 2022 02:59 PM UTC

Hi Joseph, 

Greetings from Syncfusion. 

You want to get the parent grouping Count in subgroup and want to calculate the proportions based on the parent count value. Here, we have prepared a simple sample based on your requirement. We have get the parent group count, and calculated the sub group value and displayed the calculated value in the sub group caption template. Find the below code snippets and sample for your reference. 

 
<SfGrid DataSource="@Products" AllowGrouping="true" AllowPaging="true"> 
    <GridGroupSettings Columns=@Units> 
        <CaptionTemplate> 
            @{ 
                var order = (context as CaptionTemplateContext); 
                if (order.Level == 1) 
                { 
                    TopParentCount = order.Count; 
                    <div>Parent Grouping - @order.Count</div> 
                } 
                else if (order.Level == 2) 
                { 
                    Calculated = (order.Count / TopParentCount); 
                    <div>Sub Grouping - @order.Count :- @Calculated</div>  
                } 
 
            } 
        </CaptionTemplate> 
    </GridGroupSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Product.ProductName) HeaderText="Product Name" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        . . . 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    double TopParentCount { get; set; } = 0;    // for getting parent aggregate count value 
    double Calculated { get; set; }    // for calculating value for subgroup 
    public List<Product> Products { get; set; } 
    private string[] Units = (new string[] { "QuantityPerUnit", "ProductName" }); 
    . . . 
} 
 


Please let us know if you have any concerns. 

Regards, 
Rahul 



JT Joseph Tan March 28, 2022 07:27 PM UTC

Hi there,


That won't work because 

A. when these are set:

  • EnableLazyLoading="true"
  • DisablePageWiseAggregates="true"

Level is always 1.

and 

B. The counts at the parent level in your example are wrong. 
The value of TopParentCount  = 3  but the count at the parent level is actually  5.

The correct result in your example would be :
  • Sub Grouping 2 : 0.4 
  • Sub Grouping 2 : 0.4 
  • Sub Grouping 1 : 0.2




RN Rahul Narayanasamy Syncfusion Team March 30, 2022 02:54 AM UTC

Hi Joseph,


Thanks for the update.


We are able to reproduce the case while enabling the EnableLazyLoading property. While enabling the EnableLazyLoading property all the rows are initially in the collapsed state. So the level is always 1. We suggest you to call the ExpandAllGroupAsync method in the DataBound event to perform those calculations correctly. Find the below code snippets and sample for your reference.


Reference:

https://blazor.syncfusion.com/documentation/datagrid/events#databound

https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_ExpandAllGroupAsync


 

<SfGrid @ref="Grid" DataSource="@Products" AllowGrouping="true" AllowPaging="true">

    <GridEvents DataBound="DataBoundHandler" TValue="Product"></GridEvents>

    <GridGroupSettings EnableLazyLoading="true" DisablePageWiseAggregates="true" Columns=@Units>

        <CaptionTemplate>

            @{

                var order = (context as CaptionTemplateContext);

                if (order.Level == 1)

                {

                    TopParentCount = order.Count;

                    <div>Parent Grouping - @order.Count</div>

                }

                else if (order.Level == 2)

                {

                    Calculated = (order.Count / TopParentCount);

                    <div>Sub Grouping - @order.Count :- @Calculated</div>

                }

 

            }

        </CaptionTemplate>

    </GridGroupSettings>

    <GridColumns>

        <GridColumn Field=@nameof(Product.ProductName) HeaderText="Product Name" TextAlign="TextAlign.Right" Width="120"></GridColumn>

        . . .

    </GridColumns>

</SfGrid>

 

@code{

    SfGrid<Product> Grid;

    double TopParentCount { get; set; } = 0;    // for getting parent aggregate count value

    double Calculated { get; set; }    // for calculating value for subgroup

    public List<Product> Products { get; set; }

    private string[] Units = (new string[] { "QuantityPerUnit", "ProductName" });

    . . .

 

    bool IsInitial { get; set; } = true;

    public async Task DataBoundHandler()

    {

        if (IsInitial)

        {

            await Grid.ExpandAllGroupAsync();

            IsInitial = false;

        }

    }

}


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/SampleLazy1033979247


Please let us know if you have any concerns.


Regards,

Rahul



JT Joseph Tan March 31, 2022 03:07 AM UTC

Hi Rahul,


The sample you have provided is wrong. Here is an example screen shot from your sample: 



As you can see, there are  6 items, spread across 5 groups, under this Parent Grouping.

The goal is to shown what percent of items (count)  is in each sub grouping.  The desired output (for the example above  is) 

  • Subgroup - 1 item (16.7%)
  • Subgroup - 1 item (16.7%)
  • Subgroup - 1 item (16.7%)
  • Subgroup - 2 item (33.3%)
  • Subgroup - 1 item (16.7%)


Does that make sense?



RS Renjith Singh Rajendran Syncfusion Team April 1, 2022 12:49 PM UTC

Hi Joseph,


We are checking the reported scenario from our side. We will update you further details within two business days.


Until then we appreciate your patience.


Regards,

Renjith R




RN Rahul Narayanasamy Syncfusion Team April 5, 2022 02:10 PM UTC

Hi Joseph,


Thanks for your patience.


While performing the grouping operation, we have set the grouped column Key, and Field values in CaptionTemplateContext. You can pass the CaptionTemplateContext values to a method and calculate the grouped count for the group and perform your operations. Find the below code snippets and screenshot for your reference.


 

<SfGrid @ref="Grid" DataSource="@Products" AllowGrouping="true" AllowPaging="true">

    <GridEvents DataBound="DataBoundHandler" TValue="Product"></GridEvents>

    <GridGroupSettings EnableLazyLoading="true" DisablePageWiseAggregates="true" Columns=@Units>

        <CaptionTemplate>

            @{

                var order = (context as CaptionTemplateContext);

                if (order.Level == 1)

                {

                    TopParentCount = @CalculateTotal(context as CaptionTemplateContext);

                    <div>Parent Grouping - @order.Count</div>

                }

                else if (order.Level == 2)

                {

                    Calculated = (order.Count / TopParentCount);

                    <div>Sub Grouping - @order.Count :- @Calculated</div>

                }

 

            }

        </CaptionTemplate>

    </GridGroupSettings>

    . ..

    </GridColumns>

</SfGrid>

 

@code{

    SfGrid<Product> Grid;

    double TopParentCount { get; set; } = 0;    // for getting parent aggregate count value

    double Calculated { get; set; }    // for calculating value for subgroup

    public List<Product> Products { get; set; }

    private string[] Units = (new string[] { "QuantityPerUnit", "ProductName" });

    . . .

    public double CalculateTotal(CaptionTemplateContext val)

    {

        //here, you can calculate the count value based on your requirement.

        //you can get the grouped column Key value in CaptionTemplateContext. Based on the Key value you can perform LINQ Query operations and return the total records count in the group

        . . ..

        //return calculated count value

    }

 

 

    . ..

}


Please let us know if you have any concerns.


Regards,

Rahul


Loader.
Up arrow icon