Custom link example with calling a custom function
<?php
require_once '../../../../php/demo/tabs3.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>jqGrid PHP Demo</title>
<!-- The jQuery library is a prerequisite for all jqSuite products -->
<script type="text/ecmascript" src="../../../../js/jquery.min.js"></script>
<!-- We support more than 40 localizations -->
<script type="text/ecmascript" src="../../../../js/trirand/i18n/grid.locale-en.js"></script>
<!-- This is the Javascript file of jqGrid -->
<script type="text/ecmascript" src="../../../../js/trirand/jquery.jqGrid.min.js"></script>
<!-- This is the localization file of the grid controlling messages, labels, etc.
<!-- A link to a jQuery UI ThemeRoller theme, more than 22 built-in and many more custom -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- The link to the CSS that the grid needs -->
<link rel="stylesheet" type="text/css" media="screen" href="../../../../css/trirand/ui.jqgrid-bootstrap.css" />
<script>
$.jgrid.defaults.width = 780;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';
function MyLink( p1, p2)
{
alert( "p1= "+p1 + " , "+ "p2="+ p2);
}
</script>
</head>
<body>
<div style="margin-left:20px">
<?php include ("grid.php");?>
</div>
<br/>
<?php tabs(array("grid.php"));?>
</body>
</html>
grid.php.
<?php
require_once '../../jq-config.php';
// include the jqGrid Class
require_once ABSPATH."php/PHPSuito/jqGrid.php";
// include the driver class
// Create the jqGrid instance
$grid = new jqGridRender();
// Lets create the model manually and set the formatters
$Model = array(
array("name"=>"ID","width"=>50),
array("name"=>"PhotoFileName","width"=>100),
array("name"=>"Photo","width"=>100,"formatter"=>"js:formatImage", "unformat"=>"js:unformatImage"),
array("name"=>"Rating","sorttype"=>"integer","formatter"=>"js:formatRating","unformat"=>"js:unformatRating", "cellattr"=>"js:formatcell"),
array("name"=>"Link","sorttype"=>"string","formatter"=>"js:formatLink"),
);
// Let the grid create the model
$grid->setColModel($Model);
// Set grid option datatype to be local
$grid->setGridOptions(array("datatype"=>"local","height"=>350));
//We can add data manually using arrays
$data = array();
for($i=0;$i<9;$i++)
{
$data[] = array("ID"=>$i+1,"PhotoFileName"=>($i+1).".jpg","Photo"=>($i+1).".jpg", "Rating"=>rand(-20,30), "Link"=>"link".($i+1));
}
// Let put it using the callGridMethod
$grid->callGridMethod("#grid", 'addRowData', array("ID",$data));
// We can put JS from php
$custom = <<<CUSTOM
function formatImage(cellValue, options, rowObject) {
var imageHtml = "<img src='images/" + cellValue + "' originalValue='" + cellValue + "' />";
return imageHtml;
}
function unformatImage(cellValue, options, cellObject) {
return $(cellObject.html()).attr("originalValue");
}
function formatRating(cellValue, options, rowObject) {
var color = (parseInt(cellValue) > 0) ? "green" : "red";
var cellHtml = "<span style='color:" + color + "' originalValue='" +
cellValue + "'>" + cellValue + "</span>";
return cellHtml;
}
function unformatRating(cellValue, options, cellObject) {
return $(cellObject.html()).attr("originalValue");
}
function formatcell(rowId, val, rawObject, cm, rdata) {
var imageHtml = "style='background-color:red;";
return imageHtml;
}
function formatLink(cellValue, options, rowData) {
return "<a href='#' onclick='MyLink("+rowData.ID+","+"\""+rowData.Photo+"\""+");'>"+cellValue+"</a>";
}
CUSTOM;
// Let set the code which is executed at end
$grid->setJSCode($custom);
$grid->renderGrid('#grid','#pager',true, null, null, true,true);