It is important to note that grouping and grouping operatins are performed at client side and not at server. The only thing that should be considered is that the data should be sorted in a appropriate way from server - i.e - first on field (column) on which we want to group and then to the desired column. This is done automatically in the PHP component with the option groupDataSorted (see below).
In this example we demonstrate this feature.
Our goal is to group the Orders by Customer and then show a subtotal Freight for each Customer.
This can be done just with few lines of code and adding a summaryType to the Freight column.
Below is the code where every operation related to the grouping is explained.
<?php require_once 'jq-config.php'; // include the jqGrid Class require_once "php/jqGrid.php"; // include the driver class require_once "php/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 OrderID, OrderDate, CustomerID, Freight, ShipName FROM orders'; // 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"=>'auto', // Enable grouping "grouping"=>true, // grouping options "groupingView"=>array( // group by CustomerID "groupField" => array('CustomerID'), // show the grouped column "groupColumnShow" => array(true), // Bold the text at header "groupText" =>array('<b>{0}</b>'), // Tell the grid that it should sort the data on server in the appropriatre way "groupDataSorted" => true, // Allow summary footer to place a varios summary info "groupSummary" => array(true) ) )); // Change some property of the field(s) $grid->setColProperty("OrderID", array("label"=>"ID", "width"=>60)); $grid->setColProperty("OrderDate", array( "formatter"=>"date", "formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"m/d/Y") ) ); // Add a summary property to the Freight Column $grid->setColProperty("Freight", array("summaryType"=>"sum", summaryTpl=>"Sum: {0}", "formatter"=>"number")); // Enjoy $grid->renderGrid('#grid','#pager',true, null, null, true,true); $conn = null; ?>