Dojo Ajax Combo box in Zend Framework: Creating Ajax combo box using dojo in zend framework

9 Jan

Damn it. I wasn’t even imagining that Zend team has done such a wonderful job for creating ajax enabled application. Last night I was amazed by dojo grid and today by combo box.

I don’t have words to appreciate Zend developers.

Lets see how much time and efforts it takes to make a fabulous ajax enabled combo box using dojo and zend framework.

Create a controller and place the following code

<?

class TestController extends Zend_Controller_Action

{

function indexAction()

{

$form= new Zend_Dojo_Form();

$form->addElement(

‘ComboBox’,

‘foo’,

array(

‘label’       => ‘ComboBox (datastore)’,

‘storeId’     => ‘stateStore’,

‘storeType’   => ‘dojo.data.ItemFileReadStore’,

‘storeParams’ => array(

‘url’ => ‘records’,

),

‘dijitParams’ => array(

‘searchAttr’ => ‘name’,

),

)

);

$this->view->form=$form;

}

function recordsAction()

{

$db=Zend_Registry::get(‘db’);

$sql = ‘SELECT * FROM company’;

$result = $db->fetchAll($sql);

$dojoData= new Zend_Dojo_Data(‘id’,$result,’id’);

echo $dojoData->toJson();

exit;

}

}

We define our form as

$form = new Zend_Dojo_Form();

 

And combo box element to it. Few of it properties are important, such as storeId, storeType and storeParams. In the store Params we have given the url of the action.

Next we define our action which fetch data form the table and create Json response for us. That’s it we have done.

In view/scripts/index.phtml, only write

<? echo $this->form?>

 

Only one thing. Write following lines in your layout file

<? $this->dojo()->setDjConfigOption(‘usePlainJson’,true)

->addStylesheetModule(‘dijit.themes.tundra’)

->setLocalPath(“http://localhost/Zend/js/dojo/dojo/dojo.js&#8221;)

->addLayer(“http://localhost/Zend/js/dojo/dojo/dojo.js&#8221;);

echo $this->dojo();

?>

That’s it.  You will now see a nice and beautiful combo box sending ajax request and fetching data on the fly.

One Response to “Dojo Ajax Combo box in Zend Framework: Creating Ajax combo box using dojo in zend framework”

  1. chonny March 13, 2009 at 10:43 am #

    This example doesn’t work with zend 1.7.6 and dojo 1.2.3.
    I’ve spent 2 days to resolve problem without success😦

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: