Format sum aggregates as currency

1 Answer 415 Views
Grid
jonathan
Top achievements
Rank 1
Iron
jonathan asked on 10 Apr 2023, 06:31 PM | edited on 10 Apr 2023, 06:33 PM

I have the following in a .cshtml file:

                @(Html.Kendo().Grid<TaxCertApp.ViewModels.ViewModel>()
          .Name("FilingFeesGrid")
          .Pageable()
          .Sortable()
          .Scrollable()
          .HtmlAttributes(new { style = "height:500px;" })
          .AutoBind(false)
          .Columns(columns =>
          {
              columns.Bound(s => s.DistributionDate).Format("{0:MM/dd/yyyy}").Width(100).EditorTemplateName("Date");
              columns.Bound(s => s.Disbursed)
                .Format("{0:C2}")
                .EditorTemplateName("Disbursed")
                .Width(100)
                .ClientFooterTemplate("Total: #=sum#")
                .HtmlAttributes(new { style = "text-align: right" })
                .FooterHtmlAttributes(new { style = "text-align: right" });
              columns.Bound(s => s.Received)
                .Format("{0:C2}")
                .EditorTemplateName("Received")
                .Width(100)
                .ClientFooterTemplate("Total: #=sum#")
                .HtmlAttributes(new { style = "text-align: right" })
                .FooterHtmlAttributes(new { style = "text-align: right" });
              columns.Bound(s => s.CHECKNO).Title("Check Number").Width(100);
              columns.Bound(s => s.DESCRIPTION).Title("Description").Width(300);
          })
            .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(10)
            .Aggregates(aggregates =>
            {
                aggregates.Add(p => p.Disbursed).Sum();
                aggregates.Add(p => p.Received).Sum();
            })
            .Read(read => { read.Action("Fees", "Fees").Data("{ ID: " + @Model.ID + "}"); })
            )
                )

 

This generates a grid with 2 columns having sum aggregates.  The format of those 2 columns is currency.  How can I get the sum aggregates to display with the same currency format?  I have included a picture of the rendered grid. 

Question #2: How can I access the two aggregate sum's to display a "difference" number (the Disbursed sum - the Received sum)?

1 Answer, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 13 Apr 2023, 08:28 AM

Hello Jonathan,

Thank you for the code snippets and details provided.

In order to format an aggregate result, I would recommend using the following syntax:

columns.Bound(p => p.Freight).ClientFooterTemplate("#= kendo.toString(sum,'c')#").Format("{0:C}");
For the second question - I am not sure where the calculation should happen, but here is how to access the value of the aggregate sum from above:
    $(document).ready(function () {
        setTimeout(function () { 
            var grid = $("#grid").data("kendoGrid");
            var aggregates = grid.dataSource.aggregates().Freight;
            console.log(aggregates.sum);
        },300)
    })
I hope this information helps. Let me know if further assistance is needed.

Kind Regards,
Anton Mironov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
jonathan
Top achievements
Rank 1
Iron
Answers by
Anton Mironov
Telerik team
Share this question
or