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

Aggregates is not recalculated on update/destroy

2 Answers 191 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Aurimas
Top achievements
Rank 1
Aurimas asked on 06 May 2013, 02:12 PM
Hello,

I'm using server side aggregation. It works perfectly when reading data. But if an item is destroyed or updated the aggregate is not recalculated (my update/destroy response contains "aggregates" data). 

For this reason, I need another request to server to read data again - which is not a case in our business application. Is this a known issue? Or maybe there is a workaround for that?

Thanks.

My datasource declaration:

viewModel = kendo.observable({
        gridSource: new kendo.data.DataSource({
            serverAggregates: true,
            transport: {
                read: function(options) {
                    $.ajax({
                        url: "/ajax/getProducts",
                        type: "GET",
                        cache: false,
                        dataType: "json",
                        success: function(result) {
                            options.success(result);
                        }
                    });
                },
                destroy: function (options) {
                    var id = {id: options.data.id};
                    $.ajax({
                        url: "/ajax/deleteProduct",
                        cache: false,
                        type: "DELETE",
                        contentType: "application/json",
                        data: JSON.stringify(id),
 
                        success: function(result) {
                            options.success(result);
                        },
                        error: function(result) {
                            options.error(result);
                            viewModel.gridSource.cancelChanges();
                        }
                    });
                },
 
                update: function (options) {
                    $.ajax({
                        url: "/ajax/updateItem",
                        cache: false,
                        type: "POST",
                        contentType: "application/json",
                        data: JSON.stringify(options.data),
 
                        success: function(result) {
                            options.success(result);
                        },
                        error: function(result) {
                            options.error(result);
                        }
                    });
                }
            },
            schema: {
                data: "data",
                aggregates: "aggregates",
                model: {
                    id: "id",
                    fields: {
                        id: {
                            type: "string"
                        },
                        description: {
                            type: "string"
                        },
                        price: {
                            type: "number"
                        },
                        quantity: {
                            type: "number"
                        }
                    }
                }
            },
            aggregate: [
                { field: "totalPrice", aggregate: "sum" }
            ]
        })
});

My destroy action json response:

{"totalSize":null,"data":[],"aggregates":{"totalPrice":{"sum":51.1}}}

Thank you!


2 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 08 May 2013, 11:41 AM
Hi,

The aggregates will be updated from the response only when a read operation is performed. If reading the data again is not an option then I can suggest to use the requestEnd event to update the _aggregateResult internal field e.g.

gridSource: new kendo.data.DataSource({
    requestEnd: function (e) {
        if (e.type != "read" && e.response.aggregates) {
            this._aggregateResult = e.response.aggregates;
        }
    }
Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Aurimas
Top achievements
Rank 1
answered on 08 May 2013, 11:57 AM
Hi,

that is what I needed. Thanks a lot!

Aurimas
Tags
Data Source
Asked by
Aurimas
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Aurimas
Top achievements
Rank 1
Share this question
or