We know that when using grouping we can define a lot of custom options to perform calculations
on certain column. For this purpose a custom summaryType can be used.
This is good in most cases, but in certain situations we need to perform complex calculations
using the calculated data of more than one columns.
Example of this can be the Weighed average.
In this example we demonstrate haw this can be done
<?php
require_once '../../../../php/demo/tabs.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>jqGrid PHP Demo</title>
<link rel="stylesheet" type="text/css" media="screen" href="../../../../css/jquery-ui.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../../../css/trirand/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../../../css/ui.multiselect.css" />
<style type="text">
html, body {
margin: 0; /* Remove body margin/padding */
padding: 0;
overflow: hidden; /* Remove scroll bars on browser window */
font-size: 75%;
}
</style>
<script src="../../../../js/jquery.min.js" type="text/javascript"></script>
<script src="../../../../js/trirand/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="../../../../js/trirand/jquery.jqGrid.min.js" type="text/javascript"></script> <script type="text/javascript">
$.jgrid.no_legacy_api = true;
$.jgrid.useJSON = true;
$.jgrid.defaults.width = "700";
</script>
<script src="../../../../js/jquery-ui.min.js" type="text/javascript"></script>
</head>
<body>
<div>
<?php include ("grid.php");?>
</div>
<br/>
<?php tabs(array("grid.php"));?>
</body>
</html>
grid.php.
<?php
ini_set('display_errors', 1);
require_once '../../jq-config.php';
// include the jqGrid Class
require_once ABSPATH."php/PHPSuito/jqGrid.php";
// include the driver class
require_once ABSPATH."php/PHPSuito/DBdrivers/jqGridPdo.php";
// Connection to the server
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
// Write the SQL Query
$grid->SelectCommand = 'SELECT CONCAT(a.OrderID,a.ProductID) as Unc, a.OrderID, b.ProductName, a.Quantity, a.UnitPrice, a.Quantity*a.UnitPrice as TotalLine FROM order_details a, products b WHERE a.ProductID=b.ProductID';
// set the ouput format to json
$grid->dataType = 'json';
// Let the grid create the model from SQL query
$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl('grid.php');
// Set alternate background using altRows property
$grid->setGridOptions(array(
"rowNum"=>10,
"sortname"=>"OrderID",
"rowList"=>array(10,20,50),
"height"=>'200',
"grouping"=>true,
"groupingView"=>array(
"groupField" => array('OrderID'),
"groupColumnShow" => array(true),
"groupText" =>array('<b>{0} - {1} product(s)</b> '),
"groupDataSorted" => true,
"groupSummary" => array(true)
)
));
// Change some property of the field(s)
// define the custonm summary type and collect the needed data
$summary = <<< SUMMARY
function (value, name, record)
{
// initialize the value object
if(typeof value === 'string'){
value = {ProductSum: 0, MarketValSum: 0 };
}
// perform summary
if(record['UnitPrice']) {
value.ProductSum += parseFloat(record['UnitPrice'])*parseFloat(record['Quantity']);
}
if(record['Quantity']) {
value.MarketValSum += parseFloat(record['Quantity']);
}
return value;
}
SUMMARY;
// formatter is used to display the needed data. note the rowid==''
$formatter = <<< FORMAT
function (cellval, opts, rwdat, act) {
// get the regional options and pass it to the custom formatter
opts = $.extend({}, $.jgrid.getRegional(this, 'formatter') , opts);
// determine if we are in summary row to put the value
if (opts.rowId === '') {
if(cellval.MarketValSum !== 0) {
var val = cellval.ProductSum/cellval.MarketValSum;
return 'WAVG: '+$.fn.fmatter('number', val, opts, rwdat, act);
} else {
return '0';
}
} else {
return $.fn.fmatter('number', cellval, opts, rwdat, act);
}
}
FORMAT;
$grid->setColProperty("Unc", array("hidden"=>true));
$grid->setColProperty("OrderID", array("label"=>"OrderID", "width"=>60));
// Add a avg and summary properties
$grid->setColProperty("UnitPrice", array(
"width"=>100,
"align"=>"right",
"summaryType" => "js:".$summary,
"formatter" => "js:".$formatter
));
$grid->setColProperty("TotalLine", array("width"=>100,"align"=>"right", "summaryType"=>"avg", "summaryTpl"=>"AVG: {0}" , "summaryRound"=>2, "formatter"=>"number"));
$grid->setColProperty("Quantity", array("width"=>100,"align"=>"right", "summaryType"=>"sum", "summaryTpl"=>"{0}"));
$grid->navigator = true;
$grid->setNavOptions('navigator', array("pdf"=>true));
// Enjoy
$grid->renderGrid('#grid','#pager',true, null, null, true,true);