1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
class JqgridController extends Zend_Controller_Action
{
public function init ()
{
$this->db = Zend_Registry::get('db');
// turn on profiler
$profiler = new Zend_Db_Profiler_Firebug('DB Queries');
$profiler->setEnabled(true);
$this->db->setProfiler($profiler);
// enable debug
Bvb_Grid_Deploy_JqGrid::$debug = true;
// enable JQuery - should be part of bootstrap
ZendX_JQuery::enableView($this->view);
// set url to jqGrid library
if ( @isset(Zend_Registry::get('config')->site->jqGridUrl) ) {
Bvb_Grid_Deploy_JqGrid::$defaultJqGridLibPath = Zend_Registry::get('config')->site->jqGridUrl;
}
$this->_config = new Zend_Config_Ini('./application/grids/grid.ini', 'production');
}
/**
* Show the source code for this controller
*
*/
public function codeAction ()
{}
public function doAction ()
{
print_r($this->getRequest()->getParams());
die();
}
public function indexAction ()
{
// construct JqGrid and let it configure
$grid1 = new Bvb_Grid_Deploy_JqGrid($this->_config->toArray());
$this->configG1($grid1, $this->_getParam('onlyFromPolynesia', 'false') === 'true');
// pass grids to view and deploy() them there
$this->view->g1 = $grid1->deploy();
}
public function exportAction ()
{
// construct JqGrid and let it configure
$grid1 = Bvb_Grid::factory('Bvb_Grid_Deploy_JqGrid', $this->_config, '', // this is the defualt grid class used to render on page
array('csv' => array($this, 'configG1PostCsv'))// do post config for Csv export
);
$this->configG1($grid1, $this->_getParam('onlyFromPolynesia', 'false') === 'true');
// pass grids to view and deploy() them there
$this->view->g1 = $grid1->deploy();
$this->render('index');
}
/**
* This will run if we will export to Csv before deploy() and ajax() functions
*/
public function configG1PostCsv ($grid)
{
// we don't want this column in export
$grid->updateColumn('_action', array('hide' => true));
}
public function g1ActionBar ($id)
{
$helper = new Zend_View_Helper_Url();
$actions = array(array('href' => $helper->url(array('action' => 'do', 'what' => 'view', 'id' => $id)), 'caption' => 'View', 'class' => '{view}'), array('href' => $helper->url(array('action' => 'do', 'what' => 'edit', 'id' => $id)), 'caption' => 'Edit', 'class' => '{edit} fixedClass'), array('href' => $helper->url(array('action' => 'do', 'what' => 'delete', 'id' => $id)), 'caption' => 'Delete', 'class' => '{delete}'), array('onclick' => new Zend_Json_Expr('alert("you clicked on ID: "+jQuery(this).closest("tr").attr("id"));'), 'caption' => 'Alert Me'));
return Bvb_Grid_Deploy_JqGrid::formatterActionBar($actions);
}
public function configG1 ($grid, $onlyFromPolynesia = false)
{
////////////////// 1. define select
$select = $this->db->select()->from('City')->order('Name')->columns(array('IsBig' => new Zend_Db_Expr('IF(City.Population>500000,1,0)')))->columns(array('_action' => 'ID'));
if ( $onlyFromPolynesia ) {
$select->join('Country', 'Country.Code=City.CountryCode', array('Region'))->where('Country.Region=?', "Polynesia");
}
$grid->setSource(new Bvb_Grid_Source_Zend_Select($select));
////////////////// 2. update column options
////////////////// see Bvb documentation
////////////////// and for jqg array see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:colmodel_options
$grid->updateColumn('ID', array('title' => '#ID', 'hide' => true));
$grid->updateColumn('_action', array('search' => false, // this will disable search on this field
'order' => 1, 'title' => 'Action', 'width' => 100, 'class' => 'bvb_action bvb_first', 'callback' => array('function' => array($this, 'g1ActionBar'), 'params' => array('{{ID}}')), 'jqg' => array('fixed' => true, 'search' => false)));
$grid->updateColumn('Name', array('title' => 'City name', 'width' => 260));
$grid->updateColumn('CountryCode', array('title' => 'Country code', 'searchType' => "="));
$grid->updateColumn('District', array('title' => 'District (ucase)', 'callback' => array('function' => create_function('$text', 'return strtoupper($text);'), 'params' => array('{{District}}'))));
$grid->updateColumn('Population', array('align' => 'right', 'jqg' => array('formatter' => 'integer')));
$grid->updateColumn('IsBig', array('width' => 40, 'title' => 'Is Big City', 'align' => 'center', 'jqg' => array('formatter' => 'checkbox', 'stype' => 'select', 'searchoptions' => array(/*'defaultValue'=>'1', */'value' => array(null => 'All', 0 => 'No', 1 => 'Yes')))));
////////////////// 3. configure Bvb grid behaviour
//$grid->noFilters(1);
//$grid->noOrder(1);
$grid->setDefaultFilters(array('IsBig' => '1'));
////////////////// 4. configure jqGrid options
////////////////// for setJqgOptions see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options
////////////////// see also other Bvb_Grid_Deploy_JqGrid::setJqg*() and Bvb_Grid_Deploy_JqGrid::jqg*() methods
$grid->setExport(array(// define parameters for csv export, see Bvb_Grid::getExports
'csv' => array('caption' => 'Csv')));
$grid->setJqgParams(array('caption' => 'jqGrid Example', 'forceFit' => true, 'viewrecords' => false, // show/hide record count right bottom in navigation bar
'rowList' => array(10, 15, 50), // show row number per page control in navigation bar
'altRows' => true)// rows will alternate color
);
$grid->setJqgParam('viewrecords', true); // yet another way to set jqGrid property viewrecords
$grid->jqgViewrecords = true; // yet another way to set jqGrid property viewrecords
$grid->setBvbParams(array('id' => 'ID'));
$grid->setBvbParam('id', 'ID'); // another way to set own Bvb_Grid_Deploy_JqGrid parameter
$grid->bvbId = 'ID'; // yet another way to set own Bvb_Grid_Deploy_JqGrid parameter
$grid->bvbOnInit = 'console.log("this message will not be logged because of call to bvbClearOnInit().");';
$grid->bvbClearOnInit();
$grid->bvbSetOnInit('console.log("jqGrid initiated ! If data are remote they are not loaded at this point.");');
//$grid->bvbFirstDataAsLocal = false; // how will grid receive first data ?
////////////////// 5. set ajax ID and process response if requested
$grid->setAjax(get_class($grid));
}
}