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

Aggregated values

1 Answer 186 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 09 Mar 2014, 11:01 AM
Hi guys,

I have few queries:

1. Is it possible, that the "Aggregated Value" of "sum" is updated as soon as I change any value of the column while bulk update. Currently it is updating on saving record.
2. How can I get this "Aggregated Value", as I have to use this in my formula (used in the change event of DataSource. I have mentioned "console.log" function in my code and I need to identify total of Foo, total of Foo with bar "A", "B" and "C" individually.

My code is as below:

var grid = $("#grid").kendoGrid({
    dataSource: {
        data: [
            {foo: 10, bar: "A", total: 0},
            {foo: 20, bar: "B", total: 0},
            {foo: 5, bar: "B", total: 0},
            {foo: 35, bar: "A", total: 0}
        ],
        schema: {
            model: {
                fields: {
                    foo: { type: "number" },
                    bar: { type: "string" },
                    total: { type: "number" }
                }
            }               
        },
        
        aggregate: [
            {field: "foo", aggregate: "sum"},
            {field: "foo", aggregate: "average"}
        ],
        change: function(e) {
            var item = e.items[0];
            if (this.group().length && e.action !== "itemchange") {
                item = getFirstItem(item);
            }
             
            console.log("Total Foo: ");
            console.log("Total Foo of bar A: ");
            console.log("Total Foo of bar B: ");
            console.log("Total Foo of bar C: ");
        }
         
    },
    //groupable: true,
    editable: true,
    toolbar: ["create", "save", "cancel"],
    columns: [
        {
            field: "foo",
            
            footerTemplate: "Sum1: #= sum # || Average: #= average #"
        },
        {
            field: "bar"   
        },
        {
            field: "total"   
        }
    ]  
}).data("kendoGrid");


Thanks for the help in advance.

1 Answer, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 12 Mar 2014, 09:12 AM
Hi Komail,

Let me get straight to the answers:
  1. Yes, that could be accomplished by using the Grid's save event handler to call the DataSource's aggregate method. This will re-calculate the aggregates and trigger the Grid's dataBound event, thus making the changes visible. Triggered the dataBound event however, has an unwanted side effect - the dirty markers will disappear.
  2. The group aggregate values could be accessed through the dataSource's view method

For convenience I prepared a small example illustrating the above, I hope it helps.

Regards,
Alexander Popov
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
Alex
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or