首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态计算分组聚合

动态计算分组聚合
EN

Stack Overflow用户
提问于 2014-02-27 21:57:15
回答 1查看 272关注 0票数 0

我有3栏-总计、计数-每一列=总数/计数;

使用光滑的网格和总结每一列是不正确的,因为加权平均数。

代码语言:javascript
复制
 Total     Count      Each
 4         2          2
 3         6          .5
---------------------------
 7         8          2.5 (incorrect )

7/8 = .875

代码语言:javascript
复制
 7         8          .875 (is correct if I take the sum(total) / sum(count) as an aggrigate)

我使用的是

代码语言:javascript
复制
new Slick.Data.Aggregators.Sum("Total")
new Slick.Data.Aggregators.Sum("Each")

但是,我怎样才能计算出这2的总和,并创造出第三个总和。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-02 18:45:46

这是一个有点棘手和处理网格的通常方式,但我设法做到这一点,把你的计算中的一些全局变量。

代码语言:javascript
复制
// global variables
var myGlobalCount = 0; 
var myGlobalTotal = 0;


function sumTotalsFormatter(totals, columnDef) {
    // keep the total inside the global variable 
    // this piece of code is new and outside of typical SlickGrid implementation
    switch(columnDef.field){
        case 'Count':   
            myGlobalCount = parseFloat(totals.sum[columnDef.field]);
            break;
         case 'Total':   
            myGlobalTotal = parseFloat(totals.sum[columnDef.field]);
            break;
    }

    // display as usual the Sum of whichever column that is
    // there is nothing new here, it's the simple SlickGrid sum display
    return '<span style="font-weight:bold;">'+totals.sum[columnDef.field]+'</span>';
}


function eachCalculation(totals, columnDef) {
    // VERY IMPORTANT.... The Each column has to come AFTER the other columns of Count & Total
    // the reason is simple we will use the global variables and they have to be filled prior to use them for calculation

    // do calculation with globale variables
    var eachTotal = myGlobalTotal / myGlobalCount;

    // display the calculation on the Title so that when you hover the total it will display calculation
    var titleText = "Total / Sum = " + eachTotal + " :: " + myGlobalTotal + " / " + myGlobalCount + " = " + eachTotal;

    return '<span style="font-size:9pt; font-weight:bold; color:gree" title="'+titleText+'">'+eachTotal+'</span>';
}

// Your columns definition will include your custom groupTotalsFormatter
columns1 = [
    {id:"Total", name:"Total", field:"Total", width:75, groupTotalsFormatter:sumTotalsFormatter},
    {id:"Count", name:"Count Part", field:"Count", width:100, groupTotalsFormatter:sumTotalsFormatter},
    {id:"Each", name:"Each", field:"Each", width:70, groupTotalsFormatter:eachCalculation}
];

我编辑了我的部分代码,没有用您的代码进行测试,但是它应该work...try出来,如果您有问题请告诉我.祝好运

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22081374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档