Skip to content

Instantly share code, notes, and snippets.

@gridphp
Last active June 14, 2020 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gridphp/af7ce29b851a125ad0af787ddd4d1dba to your computer and use it in GitHub Desktop.
Save gridphp/af7ce29b851a125ad0af787ddd4d1dba to your computer and use it in GitHub Desktop.
Loading grid with JSON data - https://www.gridphp.com
<?php
/**
* PHP Grid Component
*
* @author Abu Ghufran <gridphp@gmail.com> - http://www.phpgrid.org
* @version 2.0.0
* @license: see license.txt included in package
*/
// include db config
include_once("../../config.php");
// include and create object
include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
$g = new jqgrid();
// set few params
$grid["caption"] = "Working with JSON Data";
$grid["width"] = 700;
$g->set_options($grid);
$json = '[{"appName":"A","appType":3},{"appName":"b","appType":4}]';
$data = json_decode($json,true);
$cols = array();
foreach($data[0] as $k=>$v)
{
$col = array();
$col["title"] = ucwords($k);
$col["name"] = "$k";
$col["width"] = "100";
$col["editable"] = false; // this column is not editable
$col["search"] = false; // this column is not searchable
$cols[] = $col;
}
$g->set_columns($cols);
for ($i = 0; $i < count($data); $i++)
{
$data[$i]['id'] = $i+1;
}
// pass data in table param for local array grid display
$g->table = $data; // blank array(); will show no records
// custom events for rest services
$e["on_insert"] = array("on_insert", null, true);
$e["on_update"] = array("on_update", null, true);
$e["on_delete"] = array("on_delete", null, true);
$g->set_events($e);
function on_delete($data)
{
}
function on_insert($data)
{
}
function on_update($data)
{
}
// render grid
$out = $g->render("list1");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>
<link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link>
<script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
<script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
</head>
<body>
<div style="margin:10px">
<?php echo $out?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment