877-GRID-666 24/7 online support 324 10TH ST STE 203 OAKLAND CA 94607, USA

Math (e.g. Sum, Avg, Count) Operation

Math Operation
Math Operation

It’s possible to perform math operation on numeric columns with a little bit help from Javascript and “jqGridLoadComplete” event handler.

It’s important to set “footerrow” to true first.

PHP

1
$dg->set_grid_property(array("footerrow"=>true));

Javascript:

1
2
var colSum = $('#customers').jqGrid('getCol', 'creditLimit', false, 'sum'); // other options are: avg, count
$('#customers').jqGrid('footerData', 'set', { state: 'Total:', 'creditLimit': colSum });

The supported math operations are:

  • sum
  • count

By using sum and count, one can also obtain the avg using sum divided by count.

Complete Code Sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$dg = new C_DataGrid('SELECT customerName, city, state, creditLimit FROM customers', 'customerName', 'customers');
$dg->set_grid_property(array("footerrow"=>true));
$loadComplete = <<<LOADCOMPLETE
function ()
{
    var colSum = $('#customers').jqGrid('getCol', 'creditLimit', false, 'sum');
    var reccount = jQuery("#customers").jqGrid('getGridParam', 'reccount');
    var avg = Math.round(colSum/reccount * 100/100);
   
    $('#customers').jqGrid('footerData', 'set', { state: 'Total:', 'creditLimit': colSum });
}
LOADCOMPLETE
;
$dg->add_event("jqGridLoadComplete", $loadComplete);
$dg -> display();

See Live Example!