我有3栏-总计、计数-每一列=总数/计数;
使用光滑的网格和总结每一列是不正确的,因为加权平均数。
Total Count Each
4 2 2
3 6 .5
---------------------------
7 8 2.5 (incorrect )7/8 = .875
7 8 .875 (is correct if I take the sum(total) / sum(count) as an aggrigate)我使用的是
new Slick.Data.Aggregators.Sum("Total")
new Slick.Data.Aggregators.Sum("Each")但是,我怎样才能计算出这2的总和,并创造出第三个总和。
发布于 2014-03-02 18:45:46
这是一个有点棘手和处理网格的通常方式,但我设法做到这一点,把你的计算中的一些全局变量。
// 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出来,如果您有问题请告诉我.祝好运
https://stackoverflow.com/questions/22081374
复制相似问题