This question is locked. New answers and comments are not allowed.
I have Telerik MVC grid and have 2 groups. and i wants to do sum for each group level and total. Here is my code.
View Code
Here is my Controller and helper code.
View Code
<%= Html.Telerik().Grid<OutsourceSummary>() .Name("OutsourceSummaryGRid") .DataKeys(DataKeys => DataKeys.Add(o => o.FormatID).RouteKey("FormatId")) .DataBinding(dataBinding => dataBinding .Ajax() .Select("_OutsourcedSummary", "Job", new { JobId = Model.JobID)) .Columns(columns => { columns.Bound(o => o.FormatID).Width(80).ReadOnly(true).Hidden(true); columns.Bound(o => o.FormatName).Width(350).Sortable(false); columns.Bound(o => o.Quantity) .Aggregate(aggreages => aggreages.Sum()) .Format("{0:n0}") .ClientFooterTemplate("Total : <#= typeof Sum != 'undefined' ? $.telerik.formatString('{0:n0}',Sum) : 0 #>") .ClientGroupFooterTemplate("<div style=\"width:100%; text-align:right\"> Total : <#= typeof Sum != 'undefined' ? $.telerik.formatString('{0:n0}',Sum) : 0 #></div>") .FooterHtmlAttributes(new { @style = "text-align:right;" }) .HeaderHtmlAttributes(new { @style = "text-align:right;" }) .Width(150) .Sortable(false) .Filterable(false) .Title("Quantity") .HtmlAttributes(new { align = "right" }); }) .Pageable(page => page.PageSize(100)) .Sortable() .Filterable() .EnableCustomBinding(true) .Groupable(settings => settings .Groups(groups => groups.Add(o => o.FacilityName)) .Groups(groups => groups.Add(o => o.Operation)) .Visible(false)) %> Here is my Controller and helper code.
[GridAction(EnableCustomBinding = true)] public ActionResult _OutsourcedSummary(int JobId, GridCommand command) { int listcnt = 0; IEnumerable<OutsourceSummary> data = _FormatRepo.GetOutsourcedSummary(0, JobId); var data1 = (IQueryable<OutsourceSummary>)data.AsQueryable(); data1 = data1.ApplyPaging(command.Page, command.PageSize); listcnt = data.Count(); var aggregates = new Dictionary<string, object>(); if (command.Aggregates.Any()) { aggregates["Quantity"] = new { Sum = data1.Sum(p => p.Quantity) }; } if (command.GroupDescriptors.Any()) { var groupdata = AppendOutsourceGrouping(data, command.GroupDescriptors); return View(new GridModel { Data = groupdata, Total = listcnt, Aggregates = aggregates }); } else { return View(new GridModel { Data = data, Total = listcnt, Aggregates = aggregates }); } } private IEnumerable AppendOutsourceGrouping(IEnumerable<OutsourceSummary> data, IList<GroupDescriptor> groupDescriptors) { Func<IEnumerable<OutsourceSummary>, IEnumerable<AggregateFunctionsGroup>> selector = null; foreach (var group in groupDescriptors.Reverse()) { if (selector == null) { if (group.Member == "FacilityName") { selector = jobs => BuildInnerGroup(jobs, o => o.FacilityName); } else if (group.Member == "Operation") { selector = jobs => BuildInnerGroup(jobs, o => o.Operation); } } else { if (group.Member == "FacilityName") { selector = BuildGroup(o => o.FacilityName, selector); } else if (group.Member == "Operation") { selector = BuildGroup(o => o.Operation, selector); } } } return selector.Invoke(data).ToList(); } private static Func<IEnumerable<OutsourceSummary>, IEnumerable<AggregateFunctionsGroup>> BuildGroup<T>(Func<OutsourceSummary, T> groupSelector, Func<IEnumerable<OutsourceSummary>, IEnumerable<AggregateFunctionsGroup>> selectorBuilder) { var tempSelector = selectorBuilder; return g => g.GroupBy(groupSelector) .Select(c => new AggregateFunctionsGroup { Key = c.Key, HasSubgroups = true, Items = tempSelector.Invoke(c), }); } private static IEnumerable<AggregateFunctionsGroup> BuildInnerGroup<T>(IEnumerable<OutsourceSummary> group, Func<OutsourceSummary, T> groupSelector) { return group.GroupBy(groupSelector) .Select(i => new AggregateFunctionsGroup { Key = i.Key, Items = i.ToList(), ItemCount = i.Count() }); } I tried to do aggregates but its only doing at top level, not at group level.
Appriciate for any help.
Thanks,