There are situations where using the build in formatters and custom java script, which are executed at client side, does not meet the requirments of the application.

For this purpose we have created a customFunc, where will be possible to use a PHP function to format certain column(s).

Based on this tutorial we will create a script to format the Freight value using number_format PHP function and put some additional HTML content inside this column when the value is less than 1.

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


grid.php

<?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 )); //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) { // gdata->rows contain the grid data rows foreach($gdata->rows as $row ) { // format using "," as decimal separator if($row->Freight < 1 ) { //color in red if the value is less than 1 $row->Freight = "<font color='red'>".number_format($row->Freight, 2, ',', ' ')."</font>"; } else { $row->Freight = number_format($row->Freight, 2, ',', ' '); } } // return the modified return $gdata; } ?>