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
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?
|
<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" });
. . .
} |
|
|
Hi there,
That won't work because
A. when these are set:
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
|
<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
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)
Does that make sense?
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
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