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

Access group aggregates in group header in razor

5 Answers 648 Views
Grid
This is a migrated thread and some comments may be shown as answers.
tarnegur
Top achievements
Rank 1
tarnegur asked on 20 Jun 2015, 09:02 PM

Hi.

I have a grid written in razor, using server data source (not ajax) with grouping and aggregated columns on the group.

 Now, how can I access all columns aggregates in the GroupHeaderTemplate method?

5 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 24 Jun 2015, 08:24 AM
Hello Stas,

The GroupHeaderTemplate contains local variable "item" which contains all available aggregates for current column. Please check the example below:

columns.Bound(p => p.UnitsInStock)
    .GroupHeaderTemplate(@<text>
                @item.Title: @item.Key
            (Count: @item.Count) (UnitsOnOrder Average: @item.Average)
                </text>);})

.DataSource(dataSource => dataSource
    .Server()
    .Aggregates(aggregates =>
    {
        aggregates.Add(p => p.UnitsInStock).Min().Max().Count();
        aggregates.Add(p => p.UnitsOnOrder).Average();

Regards,
Vladimir Iliev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
tarnegur
Top achievements
Rank 1
answered on 24 Jun 2015, 09:07 AM

Hello Vladimir.

Thank you for your answer. I know this, but I need to access ALL columns aggregates.

How can I access them in one group header?

.Aggregates(groupAggregates =>
              {
                  groupAggregates.Add(x => x.Amount).Sum();
                  groupAggregates.Add(x => x.Net).Sum();
                  groupAggregates.Add(x => x.Gros).Sum();
                  groupAggregates.Add(x => x.Total).Sum();
              })

@item.Sum will be only the aggregate of one of them.

0
Vladimir Iliev
Telerik team
answered on 24 Jun 2015, 02:14 PM
Hello Stas,

Please note that you can't access the other fields duplicated aggregates (in server binding mode) as they are overridden by the current field aggregate. If you need to access all of the available aggregates then you should switch to ajax-binding mode.

Regards,
Vladimir Iliev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
tarnegur
Top achievements
Rank 1
answered on 24 Jun 2015, 06:35 PM
Do you have an example, how to make it in an ajax bound grid?
0
Dimo
Telerik team
answered on 26 Jun 2015, 03:38 PM
Hello Stas,

There is no way to access all aggregate values from all column directly from inside a specific column's group header template, however, you can use script expressions inside the template to get the Grid object, then the Grid dataSource and obtain all aggregate values by using the DataSource API.

Here is a simplified example, based on the Aggregates demo. The result is shown on the attached screenshot.

http://demos.telerik.com/aspnet-mvc/grid/aggregates

http://docs.telerik.com/kendo-ui/framework/templates/overview

http://docs.kendoui.com/api/framework/datasource#methods-aggregates


@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.UnitsInStock)
            .ClientGroupHeaderTemplate("Units In Stock: #= value # (Count: #= count#) #= kendo.stringify(grid.dataSource.aggregates()) #")
            .ClientFooterTemplate("<div>Min: #= min #</div><div>Max: #= max #</div>");
    })
)
 
<script>
 
    var grid;
 
    $(function () {
        grid = $("#Grid").data("kendoGrid");
    });
 
</script>


Regards,
Dimo
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
tarnegur
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
tarnegur
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or