Example #09: Custom aggregates functions - client side (Ajax)
  • Example #9.4: NHibernate QueryOver API and via an inherited class

1. Here we can send our view to the client, no need to pass any data yet.
public ActionResult CustomAggregatesFunctionsClientSide()
{
    return View();
}
2. NHibernate QueryOver API and via an inherited class version:
[ApplyTransaction, GridAction(EnableCustomBinding = true)]
public ActionResult CustomAggregatesFunctionsNHibernateClientSideData(GridCommand command)
{
    var query = NHibernateHelper.CurrentSession.QueryOver<Product>().Fetch(x => x.Category).Eager;

    var gridHelper = new NHibernateGridCustomBindingHelper<Product, ProductModel>(command, query)
        .AddAggregateFunction<NHibernateAggregateFunction>(product => product.UnitsOnOrder);

    var gridModel = gridHelper.BuildGridModel();

    return View(gridModel);
}
3. The NHibernateAggregateFunction class
class NHibernateAggregateFunction : NHibernateGridCustomBindingHelper<Product, ProductModel>.CustomAggregateFunction
{
    public override IFutureValue<object> GetValue(IQueryOver<Product> entities)
    {
        return entities.Multiply(x => x.UnitPrice, x => x.UnitsOnOrder).ThenSum().FutureValue<object>();
    }

    public override object GetValueFromGroup(IEnumerable<ProductModel> @group)
    {
        return @group.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.CustomAggregatesFunctionsNHibernateClientSideData(null)))
        .Groupable(settings => settings.Groups(groups => groups.Add(o => o.UnitsInStock)).Visible(false)))

Last edited Mar 8, 2012 at 9:51 PM by Luis_Fernando, version 2

Comments

No comments yet.