This example demonstrate how to integrate jqGrid with jqForm.
The customer form is created with visual jqForm Builder.
Enjoy
<?php
require_once '../../../../php/demo/tabs.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>jqGrid PHP Demo</title>
<link rel="stylesheet" type="text/css" media="screen" href="../../../../css/jquery-ui.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../../../css/trirand/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../../../css/ui.multiselect.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../../../css/trirand/ui.jqform.css" />
<style type="text">
html, body {
margin: 0; /* Remove body margin/padding */
padding: 0;
overflow: hidden; /* Remove scroll bars on browser window */
font-size: 75%;
}
#tags {z-index: 900}
.input-ui {padding :4px;}
.select-ui {padding : 3px;}
.textarea-ui {padding: 3px;}
.globalparam {display:none;}
.ui-input {padding :4px;}
.ui-select {padding : 3px;}
.ui-textarea {padding: 3px;}
.ui-event-cell {padding: 5px;height:25px;white-space: nowrap; overflow: hidden;}
.ui-grid-footer { background-image: none; font-weight: normal; text-align: left;}
</style>
<script src="../../../../js/jquery.min.js" type="text/javascript"></script>
<script src="../../../../js/trirand/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="../../../../js/trirand/jquery.jqGrid.min.js" type="text/javascript"></script> <script type="text/javascript">
$.jgrid.no_legacy_api = true;
$.jgrid.useJSON = true;
$.jgrid.defaults.width = "700";
</script>
<script src="../../../../js/jquery.form.js" type="text/javascript"></script>
<script src="../../../../js/jquery-ui.min.js" type="text/javascript"></script>
</head>
<body>
<div>
<?php include ("grid.php");?>
</div>
<br/>
<?php tabs(array("grid.php","customer.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
require_once ABSPATH."php/PHPSuito/DBdrivers/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 CustomerID, CompanyName, Phone, PostalCode, City FROM customers';
// Set the table to where you update the data
$grid->table = 'customers';
// Set output format to json
$grid->dataType = 'json';
// Let the grid create the model
$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"=>"CustomerID"
));
$grid->setColProperty('CustomerID', array("editoptions"=>array("readonly"=>true)));
// Enable navigator
$grid->navigator = true;
// Enable only editing
$grid->setNavOptions('navigator', array("excel"=>false,"add"=>false,"edit"=>false,"del"=>false,"view"=>false, "search"=>false));
// Close the dialog after editing
$grid->setNavOptions('edit',array("closeAfterEdit"=>true,"editCaption"=>"Update Customer","bSubmit"=>"Update"));
// Enjoy
$form = <<< FORM
function(){
var id = $("#grid").jqGrid('getGridParam','selrow'), data={};
if(id) {
data = {CustomerID:id};
} else {
alert('Please select a row to edit');
return;
}
var ajaxDialog = $('<div id="ajax-dialog" style="display:hidden" title="Customer Edit"></div>').appendTo('body');
ajaxDialog.load(
'customer.php',
data,
function(response, status){
ajaxDialog.dialog({
width: 'auto',
modal:true,
open: function(ev, ui){
$(".ui-dialog").css('font-size','0.9em');
},
close: function(e,ui) {
ajaxDialog.remove();
}
});
ajaxDialog.dialog('widget').css('font-size','12px');
}
);
}
FORM;
$buttonoptions = array("#pager",
array(
"caption"=>"Custom Edit Form",
"onClickButton"=>"js:".$form
)
);
$grid->callGridMethod("#grid", "navButtonAdd", $buttonoptions);
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
customer.php.
<?php
// Include class
include_once '../../jq-config.php';
include_once $CODE_PATH.'jqForm.php';
// Create instance
$customers = new jqForm('customers',array('method' => 'post', 'id' => 'customers'));
// Create the connection
include_once $CODE_PATH.'DBdrivers/jqGridPdo.php';
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
$conn->query("SET NAMES utf8");
$customers->setConnection( $conn);
// Set url
$customers->setUrl($SERVER_HOST.$SELF_PATH.'customer.php');
// Set parameters
$CustomerID = jqGridUtils::GetParam('CustomerID','ALFKI');
$CustomerID = is_string($CustomerID) ? (string)$CustomerID : '';
$jqformparams = array($CustomerID);
// Set SQL Command, table, keys
$customers->SelectCommand = 'SELECT CustomerID, CompanyName, Phone, PostalCode, City FROM customers WHERE CustomerID =?';
$customers->table = 'customers';
$customers->setPrimaryKeys('CustomerID');
$customers->serialKey = true;
// Set Form layout
$customers->setColumnLayout('twocolumn');
// Add elements
$customers->addElement('CustomerID','text', array('label' => 'CustomerID', 'maxlength' => '5', 'readonly' => '1', 'size' => '7', 'id' => 'customers_CustomerID'));
$customers->addElement('CompanyName','text', array('label' => 'CompanyName', 'maxlength' => '40', 'size' => '40', 'id' => 'customers_CompanyName'));
$customers->addElement('Phone','text', array('label' => 'Phone', 'maxlength' => '24', 'size' => '25', 'id' => 'customers_Phone'));
$customers->addElement('PostalCode','text', array('label' => 'PostalCode', 'maxlength' => '10', 'id' => 'customers_PostalCode'));
$customers->addElement('City','text', array('label' => 'City', 'maxlength' => '15', 'size' => '20', 'id' => 'customers_City'));
$elem_6[]=$customers->createElement('newSubmit','submit', array('value' => 'Submit'));
$elem_6[]=$customers->createElement('newButton','button', array('value' => 'Close', 'id' => 'close_modal'));
$customers->addGroup("newGroup",$elem_6, array('style' => 'text-align:right;', 'id' => 'customers_newGroup'));
// Add events
$onclicknewButton = <<< CLICKNEWBUTTON
function(event)
{
if($("#ajax-dialog") ) {
$("#ajax-dialog").remove();
}
}
CLICKNEWBUTTON;
$customers->addEvent('close_modal','click',$onclicknewButton);
// Add ajax submit events
$success = <<< SU
function( response, status, xhr) {
if(response=='success')
{
$("#grid").trigger("reloadGrid", [{current:true}]);
}
}
SU;
$customers->setAjaxOptions( array('dataType'=>null,
'resetForm' =>null,
'clearForm' => null,
'success' =>'js:'.$success,
'iframe' => false,
'forceSync' =>false) );
// Render the form
echo $customers->renderForm($jqformparams);
?>