
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?
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
0
Accepted
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:
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
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
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
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.
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.
http://dojo.telerik.com/IdEd/10
(Group half Count) value for each group.
0
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
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.
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
Hello Vachara,
The groupFooterTemplate can also accept a function. Please try to directly assign a function to it in the Grid configuration object.
Regards,
Alexander Valchev
Telerik
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
Thank you.
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
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
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.