There are situations where the data send to the grid should be obtained with a complex calculations and the query in this case can be quite tedious. In order to resolve such situations we have added a custom function (customFunc) where we can modifiy the data before it is send to the grid.

In order to ilustarate how this can be done we will use our example and demonstarte the two new options in the grid

  • customFunc
  • addUserData

With the help of these we can manipulate the final data send to the grid. customFunc is capable to modify the grid data and userdata , while addUserData can modify the userdata only (usually the summary column) send to the grid. The customFunc is called after the addUserData is set.

Now lets begin with our example.

Let suppose that our goal is to round the Fright field to 1 if it is little than 0 (zero). The summary row should be updated in the appropriate way and at end we want to add additional text field in the summary row to indicate that this is sum (intead that we can do this using callGridMethod with parameter footerData.

The customFunc accept two parameters the grid data object and the connection.
The function should return the new grid data object.


<?php // include the database connection settings/configuration 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); // instruct the database that we use utf-8 encoding $conn->query("SET NAMES utf8"); $grid = new jqGridRender($conn); // set the SQL select query $grid->SelectCommand = 'SELECT OrderID, OrderDate, CustomerID, ShipName, Freight FROM orders'; // Set output format to json $grid->dataType = 'json'; // Let the grid automatically create the model based on the SQL query $grid->setColModel(); // Set the url from where we obtain the data $grid->setUrl('grid.php'); // Set some grid options $grid->setGridOptions(array( "rowNum"=>10, "rowList"=>array(10,20,30), "sortname"=>"OrderID", "footerrow"=>true, "userDataOnFooter"=>true )); // Let add the Label Total to the Shipname $grid->addUserData(array("ShipName"=>"Total:")); //Define the custom function $grid->customFunc = "FrightUpdate"; $grid->renderGrid('#grid','#pager',true, null, null, true,true); $conn = null; /// here is the function function FrightUpdate($gdata, $conn) { $sumFreight = 0; // gdata->rows contain the grid data rows foreach($gdata->rows as $row ) { if($row->Freight < 1 ) { //NOTE THAT YOU CAN ADD A HTML content to the coulmns $row->Freight = 1; } $sumFreight += $row->Freight; } // set the sumary field $gdata->userdata['Freight'] = $sumFreight; // return the modified return $gdata; } ?>