Zend Framework

Archive for January 9th, 2009

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

Posted by Faheem Abbas on January 9, 2009

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”)

->addLayer(“http://localhost/Zend/js/dojo/dojo/dojo.js”);

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.

Posted in Zend Framework | 1 Comment »