Mitchell Skurnik
Mitchell Skurnik

Reputation: 1489

Using aggregate functions with Kendo UI MVC Grid wrapper

Specs

Kendo: 2012.3.1114

.Net: 4.5

MVC: 4.0

Problem

I am binding my grid using a DataTable as the Model and I need to have aggregate values. If I use the snippet below as my base (taken from the Kendo UI Code Library) there seems to be no way to set up the aggregate functions.

@(Html.Kendo().Grid(Model)
    .Name("Grid")    
    .Columns(columns => {
        foreach (System.Data.DataColumn column in Model.Columns)
        {
            columns.Bound(column.DataType, column.ColumnName);
        }
    })
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .Groupable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Read", "Home"))   
    )
)

Back in the days of the Telerik MVC controls I could set up the aggregate function you could setup the aggregate while adding the bound column but in the Kendo UI wrapper that has been moved down to be inside of the DataSource.

Telerik Grid:

columns.Bound("ColumnName").Aggregate(aggregates => aggregates.Count().Min().Max())

If I try and set up the agregate down in the DataSource I get a lovely exception "'count' is undefined" which is a bit vague.

if (column.ColumnName == "ProductID")
{
    columns
        .Bound(column.DataType, column.ColumnName)
        .ClientFooterTemplate("Count: #=count#");
}
...
.Aggregates(aggregates =>
{
aggregates.Add(a => "ProductID").Count();
})

Is there any way to get around the aggregate problem?

Upvotes: 3

Views: 8041

Answers (1)

Mitchell Skurnik
Mitchell Skurnik

Reputation: 1489

Though this does not solve the problem with doing sum functions on the datagrid, this answer was provided by the Kendo UI/Telerik support team, and does resolve the question asked above about the count function:

@(Html.Kendo().Grid<dynamic>()
    .Name("Grid")
    .Columns(columns =>
    {
        foreach (System.Data.DataColumn column in Model.Columns)
        {

            var boundColumn = columns.Bound(column.ColumnName);
            if (column.ColumnName == "ProductID")
            {
                boundColumn.ClientFooterTemplate("#= data.ProductID !== undefined ? ProductID.count : ''#");
            }
        }
    })
    .Pageable()
    .Sortable()
    .AutoBind(false)
    .Scrollable()
    .Filterable()
    .Groupable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model =>
            {
                foreach (System.Data.DataColumn column in Model.Columns)
                {

                    model.Field(column.ColumnName, column.DataType);
                }
            })
        .Read(read => read.Action("Read", "Home"))
    )
)

<script>
    $(document).ready(function () {
        var grid = $("#Grid").data("kendoGrid");
        grid.dataSource.aggregate(
            [
                {
                    field: "ProductID",
                    aggregate: "count"
                }
            ]);
    });
</script>

Upvotes: 8

Related Questions