Grid not working with aggregates

I'm attempting to set up a playground link to submit a ticket for another issue, but I'm running into a problem where the grid won't show any items if I define the <GridAggregates> node. If I comment it out (as it is in the link), the grid will display. I'm not sure why its not working.

Playground link: https://blazorplayground.syncfusion.com/VDLTZUVvGnLSprTx


2 Replies

TS Tomasz S. March 29, 2024 06:58 AM UTC

You have problem with

                    @foreach (var col in FreightCols)
                    {
                        <GridAggregateColumn Field="@($"Freight_{col}")" Type="AggregateType.Sum">
                            <FooterTemplate>
                                <span>@((context as AggregateTemplateContext).Sum)</span>
                            </FooterTemplate>
                        </GridAggregateColumn>
                    }
I don't see fields in source data model like "Freight_1", "Freight_2" etc


PS Prathap Senthil Syncfusion Team March 29, 2024 10:31 AM UTC

Hi Josh,


Based on your requirement, we would like to inform you that aggregation will work based on the column field. Therefore, the loop field isn't present in the Order model class, which is why you have faced the issue. To achieve your requirement, we suggest using a custom aggregate. Kindly refer to the below code snippet and sample for your reference.

 

 

@using   Syncfusion.Blazor.Grids

 

------                    @foreach (var col in FreightCols)

                    {

                        <GridAggregateColumn Field="@($"Freight_{col}")" Type="AggregateType.Custom">

                            <FooterTemplate>

                                <div>

                                    <p>Sum: @CustomAggregateFunction(col)</p>

                                </div>

                            </FooterTemplate>

                        </GridAggregateColumn>

                    }

                </GridAggregateColumns>

            </GridAggregate>

        </GridAggregates>

    </SfGrid>

}

 

@code

{

    internal bool Loaded;

    internal List<string> FreightCols = new() { "1", "2", "3" };

 

    public static List<Order> OrderData = GetOrders();

 

    private double CustomAggregateFunction(string col)

    {

        // Find the index of the column in FreightCols

        int colIndex = FreightCols.IndexOf(col);

 

        // Sum up the values in the specified column

        double sum = OrderData.Sum(order => order.Freights[colIndex]);

 

        return sum;

    }

 


Regards,
Prathap S


Loader.
Up arrow icon