How to calculate only data from group footer template?

Hi,

I need to calculate only the data from the group footer template, having two columns grouped as an example.


GridCalcGroup - Copia.png


Attachment: SFGrid_86bc8b1d.zip

13 Replies 1 reply marked as answer

SP Sarveswaran Palani Syncfusion Team June 6, 2022 04:27 PM UTC

Hi Anderson,


Greetings from Syncfusion support.


We have analyzed your query and we suspect that you need to perform calculations in Grid GroupFooterTemplate. You can use the below way to perform your own calculation. Here we have passed aggregate template context and we can get the grouped details with the help of LINQ query from grid instance and return the calculated value and display it inside the footer template. Kindly check the attached code snippet and UG documentation for your reference.


 

<SfGrid DataSource="@Orders" ID="OrdersGrid" @ref="ordersGrid" AllowGrouping="true" AllowPaging="true" AllowSorting="true"

        Toolbar="@(new List<string>() { "ExcelExport", "PdfExport" })" AllowExcelExport="true" AllowPdfExport="true">

            <GridGroupSettings Columns=@Units ShowGroupedColumn="true"></GridGroupSettings>

    <GridEvents ExcelAggregateTemplateInfo="ExcelAggregateTemplateInfoHandler"

                PdfAggregateTemplateInfo="PdfAggregateTemplateInfoHandler" OnToolbarClick="ToolbarClickHandler" TValue="Order"></GridEvents>

 

    <GridAggregates>

 

        <GridAggregate>

 

            <GridAggregateColumns>

                 <GridAggregateColumn Field=@nameof(Order.Freight) Type="AggregateType.Sum">

                    <GroupFooterTemplate>

                        @{

                            var aggregate = (context as AggregateTemplateContext);

                            var test = @CalculateTotal(context as AggregateTemplateContext);

 

                            <div>

                                <p>Total units: @aggregate.Sum</p>

                            </div>

                        }

                    </GroupFooterTemplate>

                </GridAggregateColumn>

 

            </GridAggregateColumns>

 

        </GridAggregate>

 

    </GridAggregates>

 

</SfGrid>

 

 

 

@code {

 

    public List<Order> Orders { get; set; }

 

    public SfGrid<Order> ordersGrid;

    public async Task<int?> CalculateTotal(AggregateTemplateContext val)

    {

        var a = ordersGrid.GroupSettings.Columns;

        //perform custom calulation here

        return a.Count();

    }

 

    }

 

 

}


Refer our UG Documentation:  https://blazor.syncfusion.com/documentation/datagrid/aggregates#group-and-caption-aggregate


Kindly get back to us if you need further assistance.


Regards,

Sarveswaran PK



AN Anderson June 6, 2022 10:15 PM UTC

Sorry, I didn't elaborate the question well... the custom calculation doesn't filter by the grouped columns, causing the calculations to have the same result.

Captura de tela 2022-06-06 190557.png


Thanks for your help.


Attachment: SFGrid_60e8c28b.zip


VN Vignesh Natarajan Syncfusion Team June 7, 2022 09:17 AM UTC

Hi Anderson,


Query: “the custom calculation doesn't filter by the grouped columns, causing the calculations to have the same result.


We understand your requirement and we would like to inform you that custom calculation needs to be handled in the GroupFooterTemplate function as per your requirement. By default, it will not be possible for us to calculate the average value at the source level. To calculate the average value of each group, we suggest you to perform the Group operation using the GroupBy method.


Refer to the below code example.


private async Task<string> CalculateTotal(AggregateTemplateContext val)

    {

        var a = Grid.GroupSettings.Columns;

        //returns the Grid current view records.

        var data = await Grid.GetCurrentViewRecordsAsync();

        //perform grouping action to that data.

        var t = data.GroupBy(x => (x.LojaId, x.Data));

        //perform custom calulation here

        //based on the grouped record perform the calculation.

        return (VendaRealizada / quantidadeCupom).ToString("c2");

    }


Based on the grouped records details we suggest you to handle the calculation and update the Group footer template. Please get back to us if you have further queries. 


Regards,

Vignesh Natarajan



AN Anderson June 8, 2022 02:28 PM UTC

Hi,

Grouping doesn't work, because it fetches all data.


I understand that the AggregateTemplateContext should provide the key of the "lojaId" (group) and the "data" (subgroup)...this way I can filter only the data of the group, is it possible?


private async Task<string> CalculateTotal(AggregateTemplateContext val)

    {

        int quantidadeCupom = 0;

        decimal vendaRealizada = 0M;


        var data = await Grid.GetCurrentViewRecordsAsync();


        if (val.Field == "Data")

        {

            quantidadeCupom = data.Where(x => x.LojaId == val.LojaId.Key && x.Data == val.Data.Key).Sum(x => x.QuantidadeCupom);

            vendaRealizada = data.Where(x => x.LojaId == val.LojaId.Key && x.Data == val.Data.Key).Sum(x => x.VendaRealizada);

        }

             return (vendaRealizada / quantidadeCupom).ToString("c2");

    }


Thanks for your help.



SP Sarveswaran Palani Syncfusion Team June 10, 2022 04:11 PM UTC

Hi Anderson,


Sorry for the inconvenience.


We are facing some complexities while validating the issue. Currently, we are checking this with high priority, and we will update the further details within two business days. Until then we appreciate your patience.


Regards,

Sarveswaran PK



VN Vignesh Natarajan Syncfusion Team June 20, 2022 05:34 AM UTC

Hi Anderson,


Sorry for the delay in getting back to you.


We have achieved your requirement by manually performing the Grouping operation in the custom aggregate function and displaying the calculated value in it. Refer to the below code example.


<SfGrid DataSource="@Data" @ref="Grid" Height="calc(100vh - 150px) !important" Width="100%" AllowExcelExport="true" AllowGrouping="true"

        AllowPaging="false" AllowSelection="true" AllowSorting="true" AllowFiltering="false" AllowResizing="true" AllowReordering="true">

. . . . .

    <GridAggregates>

        <GridAggregate>

            <GridAggregateColumns>

                . . . . . .

                <GridAggregateColumn Field=@nameof(Venda.TicketMedio) Type="AggregateType.Custom">

                    <GroupFooterTemplate>

                        @{

                            var aggregate = (context as AggregateTemplateContext);

                            <div>@CalculateTotal(aggregate).Result</div>

                        }

                    </GroupFooterTemplate>

                </GridAggregateColumn>

                <GridAggregateColumn Field=@nameof(Venda.TicketMedio) Type="AggregateType.Custom">

                    <FooterTemplate>

                        @Grid_TicketMedioCustomSummary()

                    </FooterTemplate>

                </GridAggregateColumn>

            </GridAggregateColumns>

        </GridAggregate>

    </GridAggregates>

</SfGrid>

 

<style>

    .e-grid .e-row.e-altrow {

        background-color: #f5f5f5;

    }

</style>

 

@code {

 

. . . . ..

    private async Task<string> CalculateTotal(AggregateTemplateContext val)

    {

        decimal check = 0;

        Venda particularGroup = new Venda();

        var data = await Grid.GetCurrentViewRecordsAsync();

        var t = data.GroupBy(x => new { LojaId = x.LojaId, Data = x.Data }).Select(v => new Venda() { LojaId = v.Key.LojaId, Data = v.Key.Data, VendaRealizada = v.Select(c => c.VendaRealizada).Sum(), QuantidadeCupom = v.Select(c => c.QuantidadeCupom).Sum() })

    .ToList();

        if (val.Field == "Data")

            particularGroup = t.Where(x => x.Data == DateTime.Parse(val.Key)).FirstOrDefault();

        else

            particularGroup = t.Where(x => x.LojaId == int.Parse(val.Key)).FirstOrDefault();

        if (particularGroup != null)

            check = particularGroup.VendaRealizada / particularGroup.QuantidadeCupom;

 

        return check.ToString("c2");

    }


Similarly, we request you to handle the grouping and filtering action in the custom function as per your requirement and display it in the Grid. Refer to the below attachment sample for your reference.


Please get back to us if you have further queries.


Regards,

Vignesh Natarajan



Attachment: BlazorGrid_ce0f6c6.zip


AN Anderson June 27, 2022 05:53 PM UTC

Hi Vignesh,

I performed several tests with the example sent, but the calculation does not match.

I believe that Grid.GetCurrentViewRecords should not bring all the data from the grid, but only the aggregated data.


Thanks for your help.

Captura de tela 2022-06-27 144231.png



SP Sarveswaran Palani Syncfusion Team June 28, 2022 03:56 PM UTC

Hi Anderson,


As an example, we have performed calculation as sample level and provided solution. We don’t have direct support to achieve your requirement by sending the aggregated data alone in template context. So, we suggested the previous solution of handling the action in own way. So, we request you to perform calculation on your own as per your requirement by handling the action externally.


Kindly get back to us if you have any further queries


Regards,

Sarveswaran PK



AN Anderson June 28, 2022 04:48 PM UTC

Hi Sarveswaran,


I understand the proposed solution, but it is insufficient solution to perform custom aggregator calculations in many groups.


Thank you for taking the time to help.



Marked as answer

SP Sarveswaran Palani Syncfusion Team June 30, 2022 03:41 PM UTC

Hi Anderson,

Thanks for the update.

It's not possible to send the aggregated data in template context so we need to handle the calculation manually on our own.

Please get back to us if you have further queries

Regards,

Sarveswaran PK



ET Etienne replied to Anderson Leal June 20, 2023 10:21 AM UTC

Hi Anderson


I was wondering did you ever find a solution to this problem. I am having the same issue.



AN Anderson replied to Etienne June 20, 2023 11:41 AM UTC

Hi Etienne,

I haven't found a workaround that I could use on the grid.



ET Etienne June 20, 2023 11:45 AM UTC

ok thanks for the response. much appreciated.

my calculation work at GroupFooterTemplate only if one group is expanded, once i expand more than one group then the calculations go off because its seems to take the sum of all the expanded groups rows.



Loader.
Up arrow icon