test calcular subtotales por grupo de filas

This commit is contained in:
dehernandezm 2025-02-11 15:18:37 -06:00
parent be3b66ebae
commit 45bdad3d8c
5 changed files with 56 additions and 11 deletions

View File

@ -107,6 +107,9 @@ function makeRenderer() {
beforeFirstKey = currentFirstKey;
}
rowKeysSub.push(arrRowKeys);
if (idx === rowKeys.length - 1) {
rowKeysSub.push([currentFirstKey, '[:sub-total:]']);
}
}
} else {
rowKeysSub = pivotData.getRowKeys();
@ -384,14 +387,13 @@ function makeRenderer() {
'tbody',
null,
rowKeysSub.map(function (rowKey, i) {
var isSubTotal = rowKey.includes('[:sub-total:]');
var totalAggregator = isSubTotal ? { value: function value() {
return 0;
} } : pivotData.getAggregator(rowKey, []);
return _react2.default.createElement(
'tr',
{ key: 'rowKeyRow' + i },
{ key: 'rowKeyRow' + i + ' ' + (isSubTotal ? ' bg-sky-50 ' : '') },
rowKey.map(function (txt, j) {
var x = spanSize(rowKeysSub, i, j);
if (x === -1) {
@ -406,13 +408,11 @@ function makeRenderer() {
rowSpan: x,
colSpan: j === rowAttrs.length - 1 && colAttrs.length !== 0 ? 2 : 1
},
txt
isSubTotal ? 'SubTotal' : txt
);
}),
colKeys.map(function (colKey, j) {
var aggregator = isSubTotal ? { value: function value() {
return 0;
} } : pivotData.getAggregator(rowKey, colKey);
var aggregator = pivotData.getAggregator(rowKey, colKey);
return _react2.default.createElement(
'td',
{
@ -421,7 +421,7 @@ function makeRenderer() {
onClick: getClickHandler && getClickHandler(aggregator.value(), rowKey, colKey),
style: valueCellColors(rowKey, colKey, aggregator.value())
},
isSubTotal ? 'a' : getFormatedValue(aggregator)
getFormatedValue(aggregator)
);
}),
_react2.default.createElement(
@ -431,7 +431,7 @@ function makeRenderer() {
onClick: getClickHandler && getClickHandler(totalAggregator.value(), rowKey, [null]),
style: colTotalColors(totalAggregator.value())
},
isSubTotal ? 'a' : getFormatedValue(totalAggregator)
isSubTotal ? '' : getFormatedValue(totalAggregator)
)
);
}),

File diff suppressed because one or more lines are too long

View File

@ -850,9 +850,54 @@ var PivotData = function () {
}, {
key: 'getAggregator',
value: function getAggregator(rowKey, colKey) {
var _this5 = this;
var agg = void 0;
var flatRowKey = rowKey.join(String.fromCharCode(0));
var flatColKey = colKey.join(String.fromCharCode(0));
if (rowKey.includes('[:sub-total:]')) {
var _resultAgg = {};
var arrRowKeys = this.rowKeys.filter(function (itmRk) {
return itmRk[0] === rowKey[0];
});
var subTotalBruto = 0;
var subTotalCount = arrRowKeys.length;
var subTotalMin = null;
var subTotalMax = null;
for (var idx = 0; idx < subTotalCount; idx++) {
var _rowKey = arrRowKeys[idx];
var _agg = this.getAggregator(_rowKey, colKey);
var _aggValue = _agg.value();
subTotalBruto = subTotalBruto + _aggValue;
if (idx === 0) {
subTotalMin = _aggValue;
subTotalMax = _aggValue;
} else {
subTotalMin = _aggValue < subTotalMin ? _aggValue : subTotalMin;
subTotalMax = _aggValue > subTotalMax ? _aggValue : subTotalMax;
}
}
_resultAgg.sum = subTotalBruto;
_resultAgg.count = subTotalCount;
_resultAgg.value = function () {
if (_this5.props.aggregatorName === 'Conteo') {
return subTotalCount;
}
if (_this5.props.aggregatorName === 'Suma') {
return subTotalBruto;
}
if (_this5.props.aggregatorName === 'Promedio') {
return subTotalBruto / subTotalCount;
}
if (_this5.props.aggregatorName === 'Mínimo') {
return subTotalMin;
}
if (_this5.props.aggregatorName === 'Máximo') {
return subTotalMax;
}
return 0;
};
}
if (rowKey.length === 0 && colKey.length === 0) {
agg = this.allTotal;
} else if (rowKey.length === 0) {

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"name": "v-react-pivottable",
"version": "0.11.51",
"version": "0.11.52",
"description": "A React-based pivot table",
"main": "PivotTableUI.js",
"files": [