Math Operation Archives | phpGrid - PHP Datagrid Create PHP grids in minutes, not hours. Fri, 06 Dec 2024 04:00:33 +0000 en-US hourly 1 CodeProjecthttps://wordpress.org/?v=7.0.3 129966043 Math (e.g. Sum, Avg, Count) Operation https://phpgrid.com/example/math-e-g-sum-avg-count-operation/ Tue, 10 Mar 2015 13:14:06 +0000 http://phpgrid.com/?p=3574 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: 12var colSum = $('#customers').jqGrid('getCol', 'creditLimit', false, 'sum'); // other options are: avg, count $('#customers').jqGrid('footerData', 'set', { state: 'Total:', 'creditLimit': colSum }); The supported math […]

The post Math (e.g. Sum, Avg, Count) Operation appeared first on phpGrid - PHP Datagrid.

]]>
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
15
16
17
18
// Always include namespace and conf.php on TOP of the script.
use phpCtrl\C_DataGrid;
require_once("/path/to/conf.php");  

$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!

The post Math (e.g. Sum, Avg, Count) Operation appeared first on phpGrid - PHP Datagrid.

]]>
3574