-
June 11th, 2012, 01:48 PM
#1
Need a telerik guru - custom aggregates
I have a Telerik Grid which has a footer that needs to display column sums. However, one of the colums' data types is TimeSpan, which isn't supported by Telerik's Sum aggregate. I need to use GridBoundColumnBuilder.Aggregate() to add the aggregates. Using this article, I created a class for my custom aggregate, called SumAggregate, shown below. (note that this isn't finished- its taken from the article. It actually implements a totally different aggregate)
Code:
SumAggregate.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.Mvc;
namespace TelerikPOC.CustomAggregates
{
public class SumAggregate : AggregateFunction
{
private System.Collections.Generic.List<object> distinctValues;
/// <summary>
/// Initializes the current aggregate function to its initial
/// state ready to accumulate and merge values.
/// </summary>
/// <remarks>
/// This method is called every time the accumulation of values
/// must start over for a new subset of records from the data source.
/// </remarks>
public void Init()
{
this.distinctValues = new System.Collections.Generic.List<object>();
}
/// <summary>
/// Accumulates new argument values to the current aggregate function.
/// </summary>
/// <remarks>
/// This aggregate function accepts one argument:
/// number - a numeric value to accumulate to the aggregate function;
/// </remarks>
public void Accumulate(object[] values)
{
if (!distinctValues.Contains(values[0]))
{
distinctValues.Add(values[0]);
}
}
/// <summary>
/// Merges the specified aggregate function to the current one.
/// </summary>
/// <param name="Aggregate">
/// Specifies an aggregate function to be merged to the current one.
/// </param>
/// <remarks>
/// This method allows the reporting engine to merge two accumulated
/// subsets of the same aggregate function into a single result.
/// </remarks>
public void Merge(AggregateFunction aggregate)
{
// Accumulate the values of the specified aggregate function.
System.Collections.Generic.List<object> sums1 = ((SumAggregate)aggregate).distinctValues;
foreach (object o in sums1)
{
this.Accumulate(new object[] { o });
}
}
/// <summary>
/// Returns the currently accumulated value of the aggregate function.
/// </summary>
/// <returns>
/// The currently accumulated numeric value of the aggregate function.
/// </returns>
public object GetValue()
{
return this.distinctValues.Count;
}
}
}
And here is the code for adding the aggregates. This conflicts with the code below from index.cshtml, but I wanted to include both methods of adding aggregates just to give more options on the answer. This needs to be modified to use a custom aggregate rather than a built-in one, like it is using now.
GridHelper.cs (unfinished -- will add logic to loop through the columns and such later. By the way, if anyone would like to help me out with that too, I'd be very grateful, though I'll admit I haven't tried anything yet.)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.Mvc.UI.Fluent;
using TelerikPOC.CustomAggregates;
namespace TelerikPOC.Helpers
{
public class GridHelper
{
public static void AddAggregateToColumn(GridBoundColumnBuilder<dynamic> columnBuilder, string Aggregate)
{
switch (Aggregate)
{
case "Sum":
{
columnBuilder.Aggregate(aggregates => aggregates.Sum())
.GroupFooterTemplate(result => "Sum:" + result.Sum)
.ClientFooterTemplate("Sum: <#= Sum #>")
.FooterTemplate(result => "Total: " + result.Sum);
}
break;
}
}
}
}
And then I'm using the HtmlHelper class to build/render the telerik grid, like this:
From Index.cshtml: (be sure to read the comments to the right)
@{
Html.Telerik()
.Grid(Model)
.Name("statisticalGrid")
.Columns(columns =>
{
columns.Bound(o => o.PlanID).Aggregate(something); //This is probably going to be where
columns.Bound(o => o.SessionID).Aggregate(something); //I need the help. Just not sure
columns.Bound(o => o.TimeSpan).Aggregate(something); //how to reference the custom
columns.Bound(o => o.TimeSpanDouble).Aggregate(something); //aggregate here, in
}) //place of `something`
.Sortable(sortable => sortable.Enabled(true))
.Filterable()
.Pageable(page => page.PageSize(25))
.Reorderable(reorder => reorder.Columns(true))
.Groupable(groupable => groupable.Enabled(true))
.ClientEvents(events => events
.OnColumnReorder("onReorder"))
.Render();
}
So I guess basically the question is how to reference my custom aggregate in telerik's Aggregate() method. And if you notice anything else I'm doing wrong, feel free to point it out
Edit: Just noticed that I have to implement the method CreateAggregateExpression(Expression, bool) in the SumAggregate class. Not entirely sure how to implement it, though.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|