This is a migrated thread and some comments may be shown as answers.

Doing math on aggregates

12 Answers 1273 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Brian Vallelunga
Top achievements
Rank 1
Brian Vallelunga asked on 24 Apr 2012, 05:11 PM
I need a couple of my aggregate fields to be calculated based on the aggregates of other columns. Is this possible?

For example, if I have the following fields:

Revenue, Cost, Margin (Revenue - Cost), Margin % (Margin / Revenue)

For aggregating these fields, sum works fine on Revenue, Cost, and Margin. However, if I want to find the total Margin %, I need to do: SUM(Margin) / SUM(Revenue). Is there a way to do this with the DataSource?

12 Answers, 1 is accepted

Sort by
0
Accepted
Alexander Valchev
Telerik team
answered on 26 Apr 2012, 09:27 AM
Hello Brian,

I am afraid that the dataSource currently does not support such functionality out of the box. It is possible to achieve it via custom code in the template function - for example:
{ field: "Revenue", title: "Revenue", footerTemplate: "Results: #= doMath() #"}
//...
 
function doMath() {
    var ds = $("#grid").data("kendoGrid").dataSource;
    var aggregates = ds.aggregates();
    var margin = aggregates.Revenue.sum - aggregates.Cost.sum;
    //...
    return "your result string";
}

This approach will work with pre-set dataSource aggregates though (like in this example), e.g. when groupable is not enabled.

Greetings,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Brian Vallelunga
Top achievements
Rank 1
answered on 26 Apr 2012, 03:24 PM
That works just fine, thanks.
0
Nathan
Top achievements
Rank 1
answered on 24 Apr 2014, 12:36 PM
I'm trying to achieve the same functionality.  Would you please post a complete example.  The code for the grid and the script for doing the math on the aggregate.  Also, please don't point me to the example that you mentioned in your post.  It doesn't do custom functions on the aggregate.
0
Alexander Valchev
Telerik team
answered on 29 Apr 2014, 07:45 AM
Hello Nathan,

Please check this sample page: http://trykendoui.telerik.com/IdEd/2
Inside you will find an example that demonstrates how to do math with aggregates, in this particular case the Grid displays half of the total products count.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Nathan
Top achievements
Rank 1
answered on 29 Apr 2014, 11:21 AM
Hi Alexander

This is no longer an issue for me.  Thank you for providing an example.
0
Rajesh
Top achievements
Rank 1
answered on 23 Sep 2014, 11:47 AM
Hello I'm using the example provided however I have requirement to show custom aggragated values on groupfooter as well. Can you show me how it can be achieved in below example?
http://dojo.telerik.com/IdEd/10

(Group half Count) value for each group.
0
Alexander Valchev
Telerik team
answered on 26 Sep 2014, 08:29 AM
Hello Rajesh,

To me it looks like that the example you provided is working and is displaying the Group half Count value for each group. I added the count aggregate to the group footer template and verified that the displayed values are correct. Please see the attached screen shot.

Am I missing something?

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Vachara
Top achievements
Rank 1
answered on 13 Jan 2015, 06:08 PM
Hi,

How does this example work with AngularJS?
Can I add a custom aggregate function to $scope and use it in footer template like your "doMath()" function?

Thank you.
0
Alexander Valchev
Telerik team
answered on 16 Jan 2015, 04:14 PM
Hello Vachara,

The groupFooterTemplate can also accept a function. Please try to directly assign a function to it in the Grid configuration object.

{ field: "Revenue", title: "Revenue", footerTemplate: $scope.footerTemplateConstructor }


Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Vachara
Top achievements
Rank 1
answered on 16 Jan 2015, 04:25 PM
Hi Alexander,

I could see that we can use $scope to directly return a template string.
But my point is, can we use $scope function inside said template? Something like this

// In an AngularJS controller...
{ field: "Revenue", title: "Revenue", footerTemplate: "Results: #= $scope.doMath() #"}
 
//...
  
$scope.doMath() {
     // Whatever calculation regarding the grid data
    return "your result string";
}

Thank you.
0
Alexander Valchev
Telerik team
answered on 21 Jan 2015, 09:36 AM
Hello Vachara,

This is not possible. Templates are executed in the global window scope, $scope is not available in the global window scope.

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Vachara
Top achievements
Rank 1
answered on 21 Jan 2015, 09:38 AM
Thank you.
Tags
Data Source
Asked by
Brian Vallelunga
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Brian Vallelunga
Top achievements
Rank 1
Nathan
Top achievements
Rank 1
Rajesh
Top achievements
Rank 1
Vachara
Top achievements
Rank 1
Share this question
or