Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines
You use the "grouping" feature of Table and set the "bottom" property to show subtotal at bottom of rows group. Here is the example.
Hi Paulo,
For DataTables' rowgroup please try this client-side solution first:
https://datatables.net/extensions/rowgroup/examples/initialisation/startAndEndRender.html
The code for creating DataTables widget is like:
<?php
DataTables::create(arrary(
...
"options" => array(
"order" => [[2, 'asc']],
"dataSrc" => 2,
"endRender" => "function ( rows, group ) {
var avg = rows
.data()
.pluck(5)
.reduce( function (a, b) {
return a + b.replace(/[^\d]/g, '')*1;
}, 0) / rows.count();
return 'Average salary in '+group+': '+
$.fn.dataTable.render.number(',', '.', 0, '$').display( avg );
}"
),
));
We will add rowGroup server-side option in the future for simpler setup. Thanks!
thank you Server-side would be great This totaling worked, but the totals are showing without formatting and without decimal places I have row 1: 261056 row 2: $48.06 The total of this grouping : 261056 Plus it is not right-aligning .... I've this tha works for all Datatables without grouping:
"cssClass" => array(
"table" => "table-bordered table-striped table-hover cell-border compact",
'tf' => "text-right",
"td"=>function($row,$colName) {
if (in_array($colName, array("ThisYearAllocation","paid_in_previous_year","commission_adjstment")))
{
return "text-right";
}
},
)
thank you
Hi Paulo,
To format a string/number to number format with comma please try this js function:
function numberWithCommas(x) {
return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
}
Another option is to use js native function:
let formattedNum = num.toLocaleString();
I've tested these to format numbers on client side and they work for my case. Thanks!
I tried replacing the line +avg.toFixed(0)+ with both examples and it won't show decimals, etc
David, this seems to be a bit more complicated than most of the koolreports, I am wondering if eventually the plan is to move this into a more simple approach like Tables grouping....
thanks
"endRender" => "function ( rows, group ) {
var avg = rows
.data()
.pluck(3)
.reduce( function (a, b) {
return a + b.replace(/[^\d]/g, '')*1;
}, 0);
return $('<tr/>')
.append( '<td colspan=\"3\">Total for '+group+'</td>' )
.append( '<td>'+avg.toFixed(0)+ '</td>' )
.append( '<td/>' );
}"
David, I tried both ways. I have two rows $2,562.50 + $48.06 with avg.toFixed(0) , total is 261056 with avg.toFixed(2) total is 261056.00 Here is my whole endRender function:
"endRender" => "function ( rows, group ) {
var avg = rows
.data()
.pluck(3)
.reduce( function (a, b) {
return a + b.replace(/[^\d]/g, '')*1;
}, 0);
return $('<tr/>')
.append( '<td colspan=\"3\">Total for '+group+'</td>' )
.append( '<td>'+avg.toFixed(0)+ '</td>' )
.append( '<td/>' );
}"
Is there anyway I can do this through the PHP side ? Sorry for insisting, I am hopping to stick to php as this is a PHP Reporting Framework.
Or do you have any other suggestion ? is there anything I can replace above that would fix the issue? If I do: .reduce( function (a, b) {
return a + b.replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",")*1;
}, 0);
I get NaN
thank you
Let KoolReport help you to make great reports. It's free & open-source released under MIT license.
Download KoolReport View demo