Monday, 7 November 2011

Create Dojo grid in Zend Framework- a nice example

About a month or two earlier when I used dojo calendar in my application and write article about the Zend Framework and dojo I was wondering why zend make collaboration with zend. But today when I created a grid using zend dojo I feel the real power of the dojo in Zend framework. In this article I am going to give a simple example and create a grid using Zend framework and dojo.

At the end of this tutorial we will have a grid like the following

So let’s feel the power of dojo in zend.
First execute the following query for creating a table.
CREATE TABLE 'jobs' (
    'id' int(11) NOT NULL auto_increment,
    'title' varchar(50) NOT NULL,
    'description' text,
    'posted_date' datetime default NULL,
    PRIMARY KEY  ('id')
)

This will create a table in your database.

Create a model in your models directory and write the following code in it.
class Jobs extends Zend_Db_Table_Abstract
{
    protected $_name='jobs';
    public function getJobsData()
    {
        $select = $this->_db->select()
                        ->from($this->_name);
        $results = $this->getAdapter()->fetchAll($select);
        return $results;
    }
}


and save this table as Jobs.php

In the code above, we first extend our model from Zend_Db_Table_Abstract, define its name and define our custom function getJobsData() that fetch all the rows in the jobs table. We return the results as an array.

Next create a controller JobsController in you controller’s directory and place the following code in it.
<?php
class JobsController extends Zend_Controller_Action
{
    public function  viewAction()
    {
    }
    
    public function  recordsAction()
    {
        $jobs = new Jobs();
        $data= $jobs->getJobsData();
        $dojoData= new Zend_Dojo_Data(’id’,$data,’id’);
        echo $dojoData->toJson();
        exit;
    }
}

In the above code we create two action view and records. We have placed nothing in the view action. In the recordsAction, we create an instance of Jobs model class.and then call its getJobsData() method which will give us an array of records. Next we create an instance of Zend_Dojo_Data() and give the fetched data to it. And finally we echo the data converted it into the Json.

We don’t need to create a view for the records action because we have placed exit it the end of this action which will stop rendering of the view. As we will call this action form our grid view template page so no need to define phtml file for it.

However we will need to create template file for our viewAction. In scripts/views/ directory create view.phtml file and place the following code in it.
Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
$this->dojo()->setLocalPath('http://localhost/Zend/js/dojo/dojo/dojo.js')
            ->addStyleSheetModule(’dijit.themes.tundra’)
            ->addStylesheet('http://localhost/Zend/js/dojo/dojox/grid/_grid/tundraGrid.css');
echo $this->dojo();

<script type="text/javascript">
    dojo.require("dojox.data.QueryReadStore");
    dojo.require("dojox.grid.Grid");
    dojo.require("dojo.parser");
</script>             
<body class="tundra">
    <div dojoType="dojox.data.QueryReadStore" jsId="activeStore", url="records"></div>
    <div dojoType="dojox.grid.data.DojoData" jsId="model" rowsPerPage="20″ store="activeStore"></div>
    <table id="activePastes" dojoType="dojox.grid.Grid" model="model" style="height:300px; width:700px;">
        <thead>
            <tr>
                <th field="id">Id</th>
                <th field="title">Title</th>
                <th field="description">Description</th>
                <th field="posted_date">Posted</th>
            </tr>
        </thead>
    </table>
</body>


Keep in mind this is the main template file where we are creating our gird.

In the first statement I told zend to use declarative instead of programmatic behavior which is default. Next I set local path and add specific stylesheets. Then in the javascript code I used statement

Dojo.requre(’dojox.data.’) etc

To include specific dojo js modules.

In the body tag I specify a class “tundra”. This is important for the stylesheet I have added.

Next tow div’s and table are the main code for creating grid.

The first div fetch the records from the url specified. This url may be different if you fetch records form the other controller or source. Please specify valid url otherwise you will not get what you want.

The next div act as a bridge between our first div and the table. This div take data from the first div and give it to the table for display purposes.

Keep in mind that all the attributes of the div’s and table are compulsory. Removing any of the attribute may result in unvalid records or empty page. So define everything that I have defined. Once you become able to display your records, you can play with these attributes.

No comments:

Post a Comment