.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 11th, 2012, 01:26 PM
pjschmidt3 pjschmidt3 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 1 pjschmidt3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 20 sec
Reputation Power: 0
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:
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.)

Code:
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)

Code:
@{
    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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Need a telerik guru - custom aggregates

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap