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

kendoTreeList and serverAggregates Examples?

1 Answer 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tristian Fernandez
Top achievements
Rank 1
Tristian Fernandez asked on 05 May 2015, 07:39 PM

Does anyone have a step by step example of how to use server aggregates with the kendoTreeList?  The documentation is skimpy and the examples don't work.  In my case, I'm doing lazy loads from the server with each expand.  The totals in the grid keep changing and the only way to prevent that is to have the totals come from the server or the data source before it binds.  Below is a sample.  Any help would be much appreciated.  

 

 return new kendo.data.TreeListDataSource({
        transport: {
            read: function (options) {
                var dataUrl;
                var level; // assumes that level will be hte name of the display column, and level + "ID" will be the name of the id column
                var parentId = options.data.id || null;
                        if (options.data.id == null) {
                            level = "P";
                            dataUrl = GLOBAL_WEBAPIBASEURL + "/api/Trading/GetTradeMVByGrouping?GroupBy=PositionType&StartDate=" + from + "&EndDate=" + to + "&FundId=" + GLOBAL_FUNDID + "&BUId=" + GLOBAL_BUID + "&SBUId=" + GLOBAL_SBUID;
                        } else if (options.data.id.indexOf('P') == 0) {
                            level = "D";
                            var p = options.data.id.replace('P-', '');
                            dataUrl = GLOBAL_WEBAPIBASEURL + "/api/Trading/GetTradeMVDetail?StartDate=" + from + "&EndDate=" + to + "&FundId=" + GLOBAL_FUNDID + "&BUId=" + GLOBAL_BUID + "&SBUId=" + GLOBAL_SBUID + "&PositionType=" + p;
                        }

                        //console.log('Exposure By Class ' + level);
                        //console.log(newquery);
                        $.ajax({
                            url: dataUrl,
                            dataType: "jsonp", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
                            success: function (result) {
                                var data = [];
                                var results;
                                var agg = [];
                                var totalBuy = 0, totalSell = 0, totalNet = 0;
                                for (var i in result) {
                                    switch (level) {
                                        case "P"://Parent
                                            data.push({ label: result[i].PositionType, entity: result[i].PositionType, id: level + "-" + result[i].PositionType, display: result[i].PositionType, parentId: parentId, hasChildren: true, level: 0 });
                                            break;
                                        case "D"://Detail
                                            data.push({ label: result[i].Inst, entity: result[i].Inst, entityId: result[i].InstId, id: level + "-" + result[i].InstId, display: result[i].Inst, parentId: parentId, hasChildren: false, level: 1 });
                                            break;
                                    }
                                    totalBuy        += parseFloat(result[i].Buy);
                                    totalSell       += parseFloat(result[i].Sell);
                                    totalNet        += parseFloat(result[i].Buy) - parseFloat(result[i].Sell);

                                    data[i].BuyGMV  = result[i].Buy;
                                    data[i].SellGMV = result[i].Sell;
                                    data[i].Net     = result[i].Buy - result[i].Sell;

                                }
                                agg = "{\"TotalBuyGMV\":{\"sum\":" + totalBuy + "}, \"TotalSellGMV\":{\"sum\":" + totalSell + "}, \"TotalNet\":{\"sum\":" + totalNet + "}}";
                                results= "{\"data\": " + JSON.stringify(data) + ", \"aggregates\": " + agg + "}";
                                // notify the data source that the request succeeded
                                options.success(JSON.parse(results));
                            },
                            error: function (result) {
                                // notify the data source that the request failed
                                options.error(result);
                            }
                        });

    },
        },
        schema: {
            data: "data",
            //aggregates: "aggregates",
            model: {
                id: "id",
            }
        },
        serverAggregates:true,
        aggregate: [
           { field: "TotalBuyGMV", aggregate: "sum" },
           { field: "TotalSellGMV", aggregate: "sum" },
           { field: "TotalNet", aggregate: "sum" }
        ],

        sort: { field: "Net", dir: "desc" }
    });

1 Answer, 1 is accepted

Sort by
0
Accepted
Alex Gyoshev
Telerik team
answered on 08 May 2015, 06:27 AM

Hello Tristian,

You need to return the server aggregates as a hash map (with item IDs as keys in the map).

var result = {
    data: [ { id: 1, parentId: null } ],
    aggregates: {
        null: { id: { count: 1 } },
        1: { id: { count: 1 } }
    }
};                                                     

var ds = new kendo.data.TreeListDataSource({                      
    serverAggregates: true,                            
    aggregate: { field: "id", aggregates:[ "count" ] },
    schema: {                                          
        aggregates: "aggregates"                       
    },                                                 
    transport: {                                       
        read: function(options) {                      
            options.success(result);       
        }                                              
    }                                                  
});                                                    

 

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