
There are several way we can work with Dojo Data Grid using Zend. All are very confusing until we are familier with the structure. I spent several hours on discovering the obstacle that are stopping me to get the right result on this. In this note, I am going to show a simple way on how to create a Dojo Data Grid using Zend Framework.
The resource used:

Step 1 :
First of all we need to enable Dojo in the bootstrap file.
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initDoctype() { $this->bootstrap('view'); $view = $this->getResource('view'); $view->doctype('XHTML1_STRICT'); // Adding Dojo helpers to the view $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper'); $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer(); $viewRenderer->setView($view); Zend_Controller_Action_HelperBroker::addHelper($viewRenderer); } }
Then in the layout file. If not using layout, enable the Dojo in individual view files.
layout.phtml
<?php $this->dojo()->setDjConfigOption('usePlainJson',true) ->addStylesheetModule('dijit.themes.claro') ->setLocalPath($this->baseUrl('js/dojo/dojo.js')); echo $this->dojo(); ?>
<body class="claro">
Step 2:
Create a Controller/Action that render a default page with required libraries.
// class UsermanageController extends Zend_Controller_Action // IndexAction(). public function indexAction() { // Optional contents goes here. $someContent = new Application_Form_UserManage(); $this->view->contents = $someContent; }
The above action will render the view named ‘index.phtml’. We need to append serveral scripts and stylesheet that are used by Dojo Data Grid. Inject the below code in to view file.
index.phtml
<?php $this->headLink()->appendStylesheet($this->baseUrl() . '/js/dojox/grid/resources/claroGrid.css'); ?> <?php $this->dojo()->enable(); $this->dojo()->requireModule('dojox.data.QueryReadStore') ->requireModule('dojox.grid.DataGrid'); ?>
Step 3 :
Next we need to create JSON Request URL for retrieving data from the server. For this we have to create an action in a controller with layout disabled.
public function loadusersAction() { // Load current data. $this->_helper->layout->disableLayout(); $this->_helper->viewRenderer->setNoRender(); $user = new Application_Model_DbTable_User(); $users = $user->fetchAll(); $dojoData = new Zend_Dojo_Data('id', $users, 'id'); echo $dojoData->toJson(); }
Step 4:
Capture the Data from the server in to dojo.data.QueryReadStore storage type.
ndex.phtml
<span dojoType="dojox.data.QueryReadStore" jsId="users" url="/EzDocMan/public/Usermanage/loadusers"></span>
Step 5:
Add a structure for the Data Grid using javascript within the template file.
index.phtml
Final :
Now add the div tag that is going to carry the DataGrid as follows.
index.phtml
<div id="grid1" dojoType="dojox.grid.DataGrid" store="users" structure="structure"></div>
Done!!
Full Code of used files :
layout.phtml
<?php echo $this->doctype() ?> <html xmlns="<a href="http://www.w3.org/1999/xhtml"> <head> ">http://www.w3.org/1999/xhtml"> <head> </a> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Document Library</title> <?php echo $this->headLink()->appendStylesheet($this->baseUrl('/css/global.css')); ?> <?php echo $this->headScript()->appendFile($this->baseUrl('js/ezdocman.js')); ?> <?php $this->dojo()->setDjConfigOption('usePlainJson',true) ->addStylesheetModule('dijit.themes.claro') ->setLocalPath($this->baseUrl('js/dojo/dojo.js')); echo $this->dojo(); ?> </head> <body class="claro"> <div class="content"> <?php echo $this->layout()->content; ?> </div> <?php echo $this->inlineScript() ?> </body> </html>
<?php $this->headLink()->appendStylesheet($this->baseUrl() . '/js/dojox/grid/resources/claroGrid.css'); ?> <?php $this->dojo()->enable(); $this->dojo()->requireModule('dojox.data.QueryReadStore') ->requireModule('dojox.grid.DataGrid'); ?> <div id="view-content"> <?php $this->tabContainer()->captureStart('tabcontainer', array(), array('style' => 'width:100%;height:400px;')) ?> <?php $this->contentPane()->captureStart('tab1', array(), array('title' => 'User Manager')); ?> <script type="text/javascript" > var structure = [ { field: 'id', name: 'UserID', width: '50px' }, { field: 'username', name : 'User Name', width: '100px' }, { field: 'mail', name : 'E Mail', width: '200px'} ]; </script> <span dojoType="dojox.data.QueryReadStore" jsId="users" url="/EzDocMan/public/Usermanage/loadusers"></span> <div id="grid1" dojoType="dojox.grid.DataGrid" store="users" structure="structure"></div> <?php echo $this->contentPane()->captureEnd('tab1'); ?> <?php echo $this->tabContainer()->captureEnd('tabcontainer'); ?> </div>
The above example is using simple way of building the DataGrid. There are more advanced features that can be integrated together. Please have a look at following resource to gain more understanding..
More Options & Suggestions :
If you are looking for something different then, you can try out the following Data Grids for Zend
Add new comment