grouping with aggregates

by valchev

HTML

<script src="http://cdn.kendostatic.com/2012.1.515/js/kendo.all.min.js"></script>
<link rel="stylesheet" href="http://cdn.kendostatic.com/2012.1.322/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2012.1.322/styles/kendo.default.min.css">
<button id="btnGroup" onclick="doGroup();">Group by "bar"</button>
<button id="btnGroup" onclick="clearGroup();">Clear groups</button>
<br />
<br />
<div id="grid"></div>

JavaScript

var dataSource = new kendo.data.DataSource({
    transport: {
        read: {
            //using jsfiddle echo service to simulate JSON endpoint
            url: "/echo/json/",
            dataType: "json",
            type: "POST",
            data: {
                // /echo/json/ echoes the JSON which you pass as an argument
                json: JSON.stringify([
                    {"foo":1, "bar": 1},
                    {"foo":2, "bar": 1},
                    {"foo":3, "bar": 0},
                    {"foo":4, "bar": 0},
                    {"foo":5, "bar": 0},
                    {"foo":6, "bar": 0},
                    {"foo":7, "bar": 1},
                    {"foo":8, "bar": 1},
                    {"foo":9, "bar": 0}
                ])
            }
        }
    },
    schema: {
        model: {
            id: "foo",
            fields: {
                foo: {type: "number"},
                bar: {type: "number"}              
            }
        }            
    },
    pageSize: 10
});

function doGroup () {
    groupByObj = {
        field: "bar",
        aggregates: [
            { field: "bar", aggregate: "sum" },
            { field: "bar", aggregate: "count" }
        ]
    };
    dataSource.group(groupByObj);   
}

function clearGroup() {
    dataSource.group([]); //clears groups
}

$("#grid").kendoGrid({
    dataSource: dataSource,
    sortable: { 
        mode: "multiple", // enables multi-column sorting
        allowUnsort: false
    },
    pageable: true,
    filterable: true,
    height: 400,
    scrollable: true,
    navigatable: true,
    //groupable: true,
    reorderable: true,
    resizable: true,
    selectable: "multiple",
    editable: true,
    columns: [
        { 
            field: "foo", 
            title: "Foo" 
        },
        { 
            field: "bar", 
            title: "Bar", 
            aggregates: ["sum", "count"],
            groupHeaderTemplate: "Number: #= value # (Count: #= count#) :: (Sum: #= sum#)"
  ...