Things are finally working fairly well for the customers grid (although I've not yet taken any speed issues into account). I removed the index.ctp file, since it was largely useless and I figured out how to render directly to the element. I also moved most of the jqGrid XML code into the application controller, since it will be reused by most of our controllers.
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@108 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -55,5 +55,108 @@ class AppController extends Controller {
|
||||
$this->set('sidemenu', $this->sideMenuLinks());
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* action: jqGridData
|
||||
* - Fetches the actual data requested by jqGrid as XML
|
||||
*/
|
||||
|
||||
function jqGridData($model, $records, $query) {
|
||||
// Assume we're debugging.
|
||||
// The actual jqGrid request will set this to false
|
||||
$debug = true;
|
||||
|
||||
$model_alias = $model->alias;
|
||||
$model_id = $model_alias . '.id';
|
||||
|
||||
if (isset($this->passedArgs['debug']))
|
||||
$debug = $this->passedArgs['debug'];
|
||||
|
||||
if (!$debug) {
|
||||
$this->layout = null;
|
||||
$this->autoLayout = false;
|
||||
$this->autoRender = false;
|
||||
Configure::write('debug', '0');
|
||||
}
|
||||
|
||||
// Set up some defaults
|
||||
$page = 1; // page number
|
||||
$rows = 20; // rows in the grid - rowNum parameter
|
||||
$sidx = $model_id; // sort column - index from colModel
|
||||
$sord = 'ASC'; // sort order
|
||||
|
||||
// Extract the actual settings from the jqGrid request
|
||||
if (isset($this->params['url']) && is_array($this->params['url']))
|
||||
extract($this->params['url']);
|
||||
|
||||
// Unserialize our complex structure of fields
|
||||
// This SHOULD be always set, except when debugging
|
||||
if (isset($fields))
|
||||
$fields = unserialize($fields);
|
||||
else
|
||||
$fields = array($model_id);
|
||||
|
||||
// Verify a few parameters and determine our starting row
|
||||
$total = ($records < 0) ? 0 : ceil($records/$rows);
|
||||
$page = ($page < 1) ? 1 : (($page > $total) ? $total : $page);
|
||||
$start = $rows*$page - $rows;
|
||||
|
||||
// the actual query for the grid data
|
||||
$query['group'] = $model_id;
|
||||
$query['order'] = "$sidx $sord";
|
||||
$query['limit'] = "$start, $rows";
|
||||
$results = $model->find('all', $query);
|
||||
|
||||
if ($debug) {
|
||||
ob_start();
|
||||
print_r(compact('records', 'rows', 'total', 'page', 'start'));
|
||||
}
|
||||
else {
|
||||
header("Content-type: text/xml;charset=utf-8");
|
||||
}
|
||||
|
||||
echo "<?xml version='1.0' encoding='utf-8'?>\n";
|
||||
echo "<rows>\n";
|
||||
echo " <page>$page</page>\n";
|
||||
echo " <total>$total</total>\n";
|
||||
echo " <records>$records</records>\n";
|
||||
|
||||
foreach ($results AS $result) {
|
||||
// Add the calculated fields (if any), to the model fields
|
||||
if (isset($result[0]))
|
||||
$result[$model_alias] += $result[0];
|
||||
|
||||
echo " <row id='{$result[$model_alias]['id']}'>\n";
|
||||
foreach ($fields AS $field) {
|
||||
list($tbl, $col) = explode(".", $field);
|
||||
if (isset($col))
|
||||
$data = $result[$tbl][$col];
|
||||
else
|
||||
$data = $result[$model_alias][$tbl];
|
||||
|
||||
// be sure to put text data in CDATA
|
||||
if (preg_match("/^\d*$/", $data))
|
||||
echo " <cell>$data</cell>\n";
|
||||
else
|
||||
echo " <cell><![CDATA[$data]]></cell>\n";
|
||||
}
|
||||
echo " </row>\n";
|
||||
}
|
||||
echo "</rows>\n";
|
||||
|
||||
if ($debug) {
|
||||
$xml = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
$xml = preg_replace("/&/", "&", $xml);
|
||||
$xml = preg_replace("/</", "<", $xml);
|
||||
$xml = preg_replace("/>/", ">", $xml);
|
||||
|
||||
echo ("\n<PRE>\n$xml\n</PRE>\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user