Updating Aggregate values on selection of checkbox

I have a datagrid and I have the option to individually select or select all with the checkbox, but what I am trying to do is have the Aggregate totals only update when you select a record, likewise it would need to automatically update when you un-select a record, is this possible?


3 Replies

KG Keerthana Ganesan Syncfusion Team September 22, 2022 02:41 PM UTC

Hi Simon,


Greetings from Syncfusion Support.


Query: "Update Aggregate values on the selection of checkbox"


We have checked your query and achieved your requirement with help of a custom aggregate function by using GetSelectedRecordAsync() method. You may calculate the aggregate value with your own aggregate functions, or use the custom aggregate option. Please find attached a sample and code snippet for your reference.


Counter.razor

 

<GridAggregateColumns>

     <GridAggregateColumn Field=@nameof(Product.TotalSales) Type="AggregateType.Custom">

           <FooterTemplate>

            @{

                 var test = @GetSelectedRecords();

                 <div>

                    <p>Custom:@test.Result</p>

                 </div>

              }

           </FooterTemplate>

     </GridAggregateColumn>

</GridAggregateColumns>

 

                       &

 

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

    List<Product> SelectedRecords { get; set; }

 

    public async Task<string> GetSelectedRecords()

    {

         //calculate custom aggregate operations

            

        double total = 0;

        SelectedRecords = await Grid.GetSelectedRecordsAsync();

        if (SelectedRecords?.Count > 0)

        {

                

            foreach (var Record in SelectedRecords)

            {

                total = total + Record.TotalSales ;

             }

        }

        return total.ToString();

    }


Please refer our UG Documentation : https://blazor.syncfusion.com/documentation/datagrid/aggregates#custom-aggregate


Kindly get back to us if you have further queries.


Regards,

Keerthana.


Attachment: Blazor_Aggregate_7fab168a.zip


SA Simon Arnold September 22, 2022 03:15 PM UTC

Thank you, this is perfect.


Is there any reason you used a foreach loop instead of the SelectedRecords.Sum(a => a.TotalSales);




KG Keerthana Ganesan Syncfusion Team September 23, 2022 02:27 PM UTC

Hi Simon,

Thanks for the update.

Happy to know that your issue is been resolved. There is no specific for using foreach loop. You can customize the logic as per your requirement.

Kindly get back to us if you have any further queries.

Regards,
Keerthana.


Loader.
Up arrow icon