制表器 - 计算字段 TopCalc

问题描述 投票:0回答:1

我需要计算今年相对于去年的增长((TY - LY)/LY)...我可以创建一个计算字段,但是我无法创建一个合适的函数来计算总增长。

我的桌子:

总增长值应为 ( 15 - 12 ) / 12 * 100 = 25

我的代码如下:

<html>
<body>

<div id="example-table"></div>

</body>

<script>

//define data array
var tabledata = [{'LOCNAME': 'Location_A',
'SLREVENUE': 6,
'SLREVENUE_LY': 4},
{'LOCNAME': 'Location_B',
'SLREVENUE': 9,
'SLREVENUE_LY': 8}];

//initialize table
var table = new Tabulator("#example-table", {
    data:tabledata,           //load row data from array
    layout:"fitColumns",      //fit columns to width of table
    columns:[                 //define the table columns
        {title:"Location", field:"LOCNAME"},
        {title:"Revenue", field:"SLREVENUE", topCalc:"sum"},
        {title:"Revenue_LY", field:"SLREVENUE_LY", topCalc:"sum"},
        {title:"Growth", field:"SLGROWTH", mutator:function(value, data){return (data.SLREVENUE - data.SLREVENUE_LY) / data.SLREVENUE_LY * 100;},
    }
    ],
});

</script>
</html>

请问有什么帮助吗?

我需要一个计算 topCalc 参数中增长的函数

javascript tabulator tabulator-py
1个回答
0
投票

对于标头,您需要为访问

topCalc
table.getCalcResults()
属性添加自定义函数。比如:

columns:[
    ...
    {title: 'Growth', ..., topCalc: function(_v,_d,_p) { 
            let topres = table.getCalcResults().top;
            //console.log("TopCalc top results", topres);

            return (topres["SLREVENUE"] - topres["SLREVENUE_LY"]) / topres["SLREVENUE_LY"] * 100;                
        }
    }
]

请参阅文档中的自定义计算函数计算结果

我更新了旧的Codepen我有一个额外的专栏来展示这一点。请参阅

AggregateFn
列的定义以及实现它的
getManipulatedTotal
函数。

© www.soinside.com 2019 - 2024. All rights reserved.