This is a migrated thread and some comments may be shown as answers.

[Solved] GridModel.Aggregates how to use?

2 Answers 181 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vladimir
Top achievements
Rank 2
Vladimir asked on 06 Feb 2012, 03:52 PM
Can I use GridModel.Aggregates property in Controller with custom binding for computing totals of the grid? Is there any examples?
Something like:
return View(new GridModel
{
   Data = data,
   Total = count,
   Aggregates = ???
});

2 Answers, 1 is accepted

Sort by
0
Accepted
Paul
Top achievements
Rank 1
answered on 07 Feb 2012, 09:24 PM
I ran into the same requirement.  The telerik system seems to NOT perform any fun with it. So I wrote my own as follows: 

Custom Binding in MVC Grid with AJAX loading to use the Aggregates property on the GridModel for populating aggregates (works across pages,etc).

There are 4 key pieces:

1) Modify controller to create an instance of the class that is the same as the type used in the GridModel data enumerable.  Populate that new instance with the aggregated values.
// pageItems is my paginated, filtered, sorted data.
DayBySymbolItem aggregator = new DayBySymbolItem()
                {
                    ClosingMarketValue = pageItems.Sum(a => a.ClosingMarketValue),
                    Commissions = pageItems.Sum(a => a.Commissions),
                    Dividends = pageItems.Sum(a => a.Dividends),
                    GrossPNL = pageItems.Sum(a => a.GrossPNL),
                    GrossTradeDollars = pageItems.Sum(a => a.GrossTradeDollars),
                    NetDailyPNL = pageItems.Sum(a => a.NetDailyPNL),
                    OpeningMarketValue = pageItems.Sum(a => a.OpeningMarketValue)
                };
    Data = new GridModel<DayBySymbolItem> { Total = totalRecords, Data = pageItems, Aggregates = aggregator },

2) Modify the Grid definition to include a unique class name in the FooterHtmlAttributes for which you want to push an aggregate. NO ClientTemplate is required!

            columns.Bound(o => o.NetDailyPNL).Title("Net Daily Profit and Loss")
                .Format("{0:c2}").HtmlAttributes(new { Class = "Money" })
                .FooterHtmlAttributes(new { Class = "Money NetDailyPNL" });

3) Introduce a new JS function bound to the Grid's "OnComplete" client side event with a single parameter.  This lets you grab the aggregate from the Ajax response.  Note, don't update your footer or it will get whipped by the OnDataBound.  Set the aggregate to a global var.

        .ClientEvents(events => events
            .OnDataBound("ReportLoaded")
            .OnComplete("ReportAjaxComplete")
        )

var reportAggregates = "";

function ReportAjaxComplete(e) {
    var result = e.response;
    if (result) {
        reportAggregates = e.response["aggregates"];
    }
}

function ReportLoaded(e) {
     if (reportAggregates != "") {
        UpdateAggregates(reportAggregates);
    }
}

4) Modify the OnDataBound to check to see if the aggregates exist, if so call a helper function to update the footer with the data from the aggregates. 

    function UpdateAggregates(aggs) {
        for (var key in aggs) {
            $("." + key).text( aggs[key] );
        }
   }

Done ...  (note, I format the aggregates after checking to confirm they are number value).
0
Vladimir
Top achievements
Rank 2
answered on 08 Feb 2012, 07:39 PM
Perfect! Nice workaround! Thank you very much!
Tags
Grid
Asked by
Vladimir
Top achievements
Rank 2
Answers by
Paul
Top achievements
Rank 1
Vladimir
Top achievements
Rank 2
Share this question
or