Files
pmgr/views/elements/jqGrid.ctp

335 lines
10 KiB
PHP

<?php /* -*- mode:PHP -*- */
if (!isset($caption))
$caption = '<H2>'.$this->pageTitle.'</H2>';
if (!isset($limit))
$limit = 20;
if (!isset($limitOptions))
$limitOptions = array(10, 20, 50, 200);
if (!isset($height))
$height = 'auto';
if (!isset($gridId))
$gridId = $this->params['controller'] . '-jqGrid';
// Do some prework to bring in the appropriate libraries
$imgpath = '/pmgr/site/css/jqGrid/basic/images';
$html->css('jqGrid/basic/grid', null, null, false);
$html->css('jqGrid/jqModal', null, null, false);
$javascript->link('jqGrid/jquery.jqGrid.js', false);
$javascript->link('jqGrid/js/jqModal', false);
$javascript->link('jqGrid/js/jqDnR', false);
// Helper function to convert PHP vars to javascript
function phpVarToJavascript($var, $name = '', $depth='', $special = false) {
// Establish a prefix to use before printing $var
$prefix = $depth;
// If given a name, set it up JS style
if ($name)
$prefix .= $name . ": ";
if (!isset($var))
return $prefix . 'null';
if (is_bool($var))
return $prefix . ($var ? "true" : "false");
if (is_numeric($var))
return $prefix . $var;
if (is_string($var)) {
// Check to see if this is a special
if ($special)
return $prefix . $var;
// OK, must just be a string after all
return $prefix . "'$var'";
}
// The only thing left that we know how to dump
// is an array. Die if we have anything else.
if (!is_array($var)) {
die;
return null;
}
// Keep a lookout for special cases, flagged by '--special'
// eg: array('name' => array('--special' => array('a' => 'one', 'b' => 'two')))
// GIVES: name: [ 'one', 'two' ]
// NOT: name: { --special: { a: 'one', b: 'two' } }
// eg: array('name' => array('--special' => 'varname'))
// GIVES: name: varname
// NOT: name: { --special: 'varname' }
if (isset($var['--special']) && count($var) == 1)
return phpVarToJavascript($var['--special'], $name, $depth, true);
// PHP array indices can be a mix of integer and string based.
// Just guess here, unless flagged as a special case.
if (isset($var[0]) || $special)
return ($prefix . "[\n"
. implode(",\n",
array_map('phpVarToJavascript',
array_values($var),
array(),
array_fill(0, count($var), $depth.' ')
))
. "\n$depth]");
return ($prefix . "{\n"
. implode(",\n",
array_map('phpVarToJavascript',
array_values($var), array_keys($var),
array_fill(0, count($var), $depth.' ')
))
. "\n$depth}");
}
// Define the URL to fetch data from.
// To prevent having to keep the controller and the view
// in sync on which fields need to be queried by the
// controller in order to be accessible to the view,
// we'll just pass the desired fields to the controller
// as part of the data fetch.
$url = $html->url(array('action' => 'jqGridData',
'debug' => 0,
));
// Create extra parameters that jqGrid will pass to our
// controller whenever data is requested. 'action' will
// ensure that the controller provides the correct subset
// of data, and 'fields' will allow it to return only the
// requested fields, and in the right order. Since fields
// is a complex structure (an array), we'll need to
// serialize it first for transport over HTTP.
$postData = array('action' => $action,
'fields' => serialize(array_map(create_function('$col',
'return $col["index"];'),
$jqGridColumns)));
// Perform column customizations.
// This will largely be based off of the 'formatter' parameter,
// but could be on any pertinent condition.
foreach ($jqGridColumns AS &$col) {
$default = array();
// Make sure every column has a name
$default['name'] = $col['index'];
// Perform customization based on formatter
if (isset($col['formatter'])) {
if ($col['formatter'] === 'id') {
// Switch currency over to our own custom formatting
$col['formatter'] = array('--special' => 'idFormatter');
$default['width'] = 50;
$default['align'] = 'center';
}
elseif ($col['formatter'] === 'currency') {
// Switch currency over to our own custom formatting
$col['formatter'] = array('--special' => 'currencyFormatter');
$default['width'] = 80;
$default['align'] = 'right';
}
elseif ($col['formatter'] === 'date') {
$default['formatoptions'] = array('newformat' => 'm/d/Y');
$default['width'] = 90;
$default['align'] = 'center';
}
elseif ($col['formatter'] === 'comment') {
$default['width'] = 300;
$default['sortable'] = false;
}
}
$col = array_merge($default, $col);
}
// OK, now that everything is in place, get out of PHP mode,
// and add the javascript code (along with a touch of HTML)
// to kick this thing off.
?>
<script type="text/javascript"><!--
jQuery(document).ready(function(){
currencyFormatter = function(el, cellval, opts) {
$(el).html(fmtCurrency(cellval));
}
idFormatter = function(el, cellval, opts) {
$(el).html('#'+cellval);
}
jQuery('#<?php echo $gridId; ?>').jqGrid(
<?php
echo phpVarToJavascript(array('mtype' => 'GET',
'datatype' => 'xml',
'url' => $url,
'postData' => $postData,
'colNames' => array_keys($jqGridColumns),
'colModel' => array('--special' => $jqGridColumns),
/* 'sortname' => 'id', */
/* 'sortorder' => 'ASC', */
'height' => $height,
'rowNum' => $limit,
'rowList' => $limitOptions,
'caption' => $caption,
'imgpath' => $imgpath,
'viewrecords' => true,
'pager' => array('--special' => "jQuery('#{$gridId}-pager')"),
//'toolbar' => array(true,"bottom"),
)); ?>
);
/* jQuery('#t_<?php echo $gridId; ?>').height(25).hide() */
/* .filterGrid('#<?php echo $gridId; ?>', { */
/* gridModel:true, */
/* gridToolbar:true, */
/* autosearch:true, */
/* }); */
/* jQuery('#<?php echo $gridId; ?>').navGrid('#<?php echo $gridId; ?>-pager', */
/* { view:false, */
/* edit:false, */
/* add:false, */
/* del:false, */
/* search:false, */
/* refresh:false}) */
/* .navButtonAdd('#<?php echo $gridId; ?>-pager',{ */
/* caption:"Search", */
/* title:"Toggle Search", */
/* buttonimg:'<?php echo $imgpath; ?>' + '/find.gif', */
/* onClickButton:function(){ */
/* if(jQuery('#t_<?php echo $gridId; ?>').css("display")=="none") { */
/* jQuery('#t_<?php echo $gridId; ?>').css("display",""); */
/* } else { */
/* jQuery('#t_<?php echo $gridId; ?>').css("display","none"); */
/* } */
/* } */
/* }); */
});
--></script>
<table id="<?php echo $gridId; ?>" class="scroll"></table>
<div id="<?php echo $gridId; ?>-pager" class="scroll" style="text-align:center;"></div>
<div id="debug"></div>
<script type="text/javascript"><!--
/************************************************************************
*
* Example search boxes that work with the javascript form this element
<div>Search By:<BR>
<input type="checkbox" id="autosearch" onclick="enableAutosubmit(this.checked)">Enable Autosearch<BR>
Last Name<BR>
<input type="text" CLASS="filt-text" id="filt_last_name" name="PrimaryContact.last_name" />
<button CLASS="filt-submit" ID="submit_filt_last_name" style="margin-left:30px;">Search</button><BR>
First Name<BR>
<input type="text" CLASS="filt-text" id="filt_first_name" name="PrimaryContact.first_name" />
<button CLASS="filt-submit" ID="submit_filt_first_name" style="margin-left:30px;">Search</button><BR>
<div>
<button CLASS="filt-clear" ONCLICK="gridRestore()">Clear</button>
</div>
</div>
*
*/
var timeoutHnd;
function handleFiltKeydown(e){
/* $('#debug').append("KEYDOWN: id:"+id+"<BR>"); */
if (e.which == 8 || e.which == 46 || /* Backspace / Delete */
e.which == 32 || /* Space */
(65 <= e.which && e.which < 65 + 26) || /* Upper Case */
(97 <= e.which && e.which < 97 + 26)) /* Lower Case */
{
var id = $(this).attr('id');
if(timeoutHnd)
clearTimeout(timeoutHnd);
timeoutHnd = setTimeout(function()
{gridFilter(id)},
500);
}
}
function enableAutosubmit(state) {
jQuery(".filt-submit").attr("disabled",state);
if (state) {
$(".filt-text").keydown(handleFiltKeydown);
/* $('#debug').append("BIND: id:"+id+"<BR>"); */
}
else {
$(".filt-text").unbind('keydown', handleFiltKeydown);
/* $('#debug').append("UNBIND: id:"+id+"<BR>"); */
}
}
function gridRestore() {
/* $('#debug').append("RESTORE<BR>"); */
$('.filt-text').val('');
jQuery('#<?php echo $gridId; ?>').removePostDataItem('filt');
jQuery('#<?php echo $gridId; ?>').removePostDataItem('filtField');
jQuery('#<?php echo $gridId; ?>').removePostDataItem('filtValue');
jQuery('#<?php echo $gridId; ?>')
.setGridParam({ page: 1 })
.trigger("reloadGrid");
}
function gridFilter(id) {
var field = jQuery("#"+id).attr('name');
var value = jQuery("#"+id).val();
// Make sure we're not issuing blank queries.
// It will work without observable effect, but
// it will result in slower queries. Just
// clear out all filter terms instead.
if (value == '') {
gridRestore();
return;
}
/* $('#debug').append("RELOAD: field:"+field+"; value:"+value+"<BR>"); */
jQuery('#<?php echo $gridId; ?>').setPostDataItem('filt', true);
jQuery('#<?php echo $gridId; ?>').setPostDataItem('filtField', field);
jQuery('#<?php echo $gridId; ?>').setPostDataItem('filtValue', value);
$('.filt-text').each(function(){
if ($(this).attr('id') != id)
$(this).val('');
});
jQuery('#<?php echo $gridId; ?>')
.setGridParam({ page: 1 })
.trigger("reloadGrid");
}
jQuery(document).ready(function(){
$('.filt-submit').click(function(){
gridFilter($(this).attr('id').replace(/^submit_/, ''));
});
enableAutosubmit(true);
});
--></script>