From 2817739b85ddad1b18641d530a97e6f2d6c97ac9 Mon Sep 17 00:00:00 2001 From: Andrea Bruno <21980406+Andrea-Bruno@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:15:41 +0200 Subject: [PATCH] Fix average not restricted to selected rows in DataTables footer The backend already exposed the avg-results operation, but the footer refresh for a selection only asked for summable-results, so the average kept showing the value for the whole dataset. The refresh now requests both groups and applies them in the same order as footerCallback. --- assets/src/js/functions/datatables.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/assets/src/js/functions/datatables.js b/assets/src/js/functions/datatables.js index bdf6a6d62..f304af6b1 100755 --- a/assets/src/js/functions/datatables.js +++ b/assets/src/js/functions/datatables.js @@ -548,10 +548,11 @@ function getTable(selector) { }, /** - * Nuovi valori dei campi summable + * New values for a footer totals group, restricted to the selected rows. + * @param {string} op Backend operation returning the group * @returns */ - getSelectedRowsFooter: function () { + getSelectedRowsFooter: function (op) { let ids = this.getSelectedRows(); return $.ajax({ @@ -561,26 +562,36 @@ function getTable(selector) { data: { id_module: this.id_module, id_plugin: this.id_plugin, - op: "summable-results", + op: op, ids: ids, } }); }, /** - * Aggiornamento dei campi summable + * Update the footer totals and averages for the selected rows */ updateFooterForSelectedRows: function () { let datatable = this.datatable; - this.getSelectedRowsFooter().then(function (response) { + const applyTotals = function (response, marker) { for (let [column, value] of Object.entries(response)) { let index = parseInt(column) + 1; let sel = datatable.column(index).footer(); $(sel).css("text-align", "right") - .attr("id", "summable") + .attr("id", marker) .html(value); } + }; + + // Averages are requested together with sums so both follow the selection, + // applying them in the same order as footerCallback to keep the result stable. + Promise.all([ + this.getSelectedRowsFooter("summable-results"), + this.getSelectedRowsFooter("avg-results"), + ]).then(function ([summables, averages]) { + applyTotals(summables, "summable"); + applyTotals(averages, "avg"); }); }, };