Example #09: Custom aggregates functions - client side (Ajax)
  • Example #9.2: Generic linq and via an inline function

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 via an inline function version:
public ActionResult CustomAggregatesFunctionsInlineClientSideData(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(product => product.UnitsOnOrder, products => products.Select(x => x.UnitPrice * x.UnitsOnOrder).Sum());

    var gridModel = gridHelper.BuildGridModel();

    return View(gridModel);
}
3. 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.CustomAggregatesFunctionsInlineClientSideData(null)))
        .Groupable(settings => settings.Groups(groups => groups.Add(o => o.UnitsInStock)).Visible(false)))

Last edited Mar 5, 2012 at 10:47 PM by Luis_Fernando, version 1

Comments

No comments yet.