Example #09: Custom aggregates functions - client side (Ajax)
- Example #9.3: Generic linq and ignoring group aggregates
1. Here we can send our view to the client, no need to pass any data yet.
public ActionResult CustomAggregatesFunctionsClientSide()
{
return View();
}
2. Generic linq and ignoring group aggregates version:
[ApplyTransaction, GridAction(EnableCustomBinding = true)]
public ActionResult CustomAggregatesFunctionsIgnoreGroupAggregatesClientSideData(GridCommand command)
{
// We have to use another query to count because of an issue in NHibernate:
// (currently fixed in the unreleased version 3.3.0) https://nhibernate.jira.com/browse/NH-2846
var query = NHibernateHelper.CurrentSession.Query<Product>().Fetch(x => x.Category);
var queryToCount = NHibernateHelper.CurrentSession.Query<Product>();
var gridQueryProvider = new GridQueryProvider(query, queryToCount);
var gridHelper = new GridCustomBindingHelper<Product, ProductModel>(command, gridQueryProvider)
.AddAggregateFunction<AggregateFunction>(product => product.UnitsOnOrder)
// This setting greatly reduces round trips in the database.
.IgnoreGroupAggregates();
var gridModel = gridHelper.BuildGridModel();
return View(gridModel);
}
3. The AggregateFunction class
class AggregateFunction : GridCustomBindingHelper<Product, ProductModel>.CustomAggregateFunction
{
protected override object GetValue(IQueryable<Product> entities)
{
return entities.Select(x => x.UnitPrice * x.UnitsOnOrder).Sum(x => x);
}
}
4. Finaly, the Grid configuration
@(Html.Telerik().Grid<ProductModel>().Name("Grid")
.Columns(columns =>
{
columns.Bound(x => x.ProductName)
.Aggregate(aggregates => aggregates.Count())
//.ClientGroupFooterTemplate("Count: <#= Count #>")
.ClientFooterTemplate("Total Count: <#= Count #>");
columns.Bound(o => o.UnitPrice)
.Width(200)
.Aggregate(aggreages => aggreages.Sum())
.Format("{0:c}")
//.ClientGroupFooterTemplate("Sum: <#= $.telerik.formatString('{0:c}', Sum) #>")
.ClientFooterTemplate("Total Sum: <#= $.telerik.formatString('{0:c}', Sum) #>");
columns.Bound(o => o.UnitsOnOrder)
.Width(200)
.Aggregate(aggregates => aggregates.Average())
//.ClientGroupFooterTemplate("Average: <#= Average #> </br> Order Total: <#= $.telerik.formatString('{0:c}', Custom) #>")
.ClientFooterTemplate("Average: <#= Average #> </br> All Orders Total: <#= $.telerik.formatString('{0:c}', Custom) #>");
columns.Bound(o => o.UnitsInStock)
.Width(100)
.Aggregate(aggregates => aggregates.Count().Min().Max())
//.ClientGroupHeaderTemplate("<#= Title #>: <#= Key #> (Count: <#= Count #>)")
.ClientFooterTemplate("<div>Min: <#= Min #></div>" + "<div>Max: <#= Max #></div>");
})
.EnableCustomBinding(true).Sortable().Filterable().Pageable()
.DataBinding(dataBinding => dataBinding.Ajax().Select<ExamplesController>(ctr => ctr.CustomAggregatesFunctionsIgnoreGroupAggregatesClientSideData(null)))
.Groupable(settings => settings.Groups(groups => groups.Add(o => o.UnitsInStock)).Visible(false)))