我最近发现了Tabulator.js和它的可能性,例如,在底部的一列的所有值的平均值。
我想知道是否也可以有最小值和最大值,而不是只有一个。我没有在文档中找到,但这可能是通过扩展库?(我不希望在顶部有一个,因为我需要两个以上的计算,这无论如何都不是一个解决方案。)
这与最近关于topCalc
属性的另一个答案类似。
您可以有效地在页脚行中放置任何您想要的内容。使用列定义的bottomCalc
和bottomCalcFormatter
属性以及自定义函数来获取适当的内容。
在自定义函数中,您可以使用内置函数,例如table.getCalcResult()
方法。
例如:返回一个包含sum, average, count的数组
let table = new Tabulator("#my-table-id", {
...
columns:[
...
{
title: 'My col',
...,
bottomCalc: function(v, d, p) {
// v - array of column values
// d - all table data
// p - params passed from the column definition object
let res = table.getCalcResults();
// eg Get sum and count for a column with id 'C1'
let c1_calc = 0, c1_count = 0;
d.forEach(function(row) {
c1_calc += row["c1"];
c1_count++;
});
return [
(parseInt(res.bottom["COL_1_ID"], 10) + parseInt(res.bottom["COL_2_ID"], 10)),
(parseInt(res.bottom["COL_1_ID"], 10) + parseInt(res.bottom["COL_2_ID"], 10)) / 2,
c1_calc,
c1_count
];
}
}
],
...
);
一旦你有了内容,你可以使用一个自定义的格式化器来显示它。
请参见文档中的自定义计算函数和计算结果。
我再次更新了我的Codepen,添加了bottomCalc
函数和格式化器。参见AggregateFn
列的定义、实现它的getBottomAggregate
函数和格式化它的bottomAggregateFormatter
函数。
希望对你有帮助。