Zend Framework and dojo- Auto Complete Example
Posted by Faheem Abbas on January 27, 2009
Sorry this blog is deprecated.
Find latest version of this article here. http://zendgeek.blogspot.com/2009/07/zend-framework-and-dojo-auto-complete.html
Creation of auto complete in Zend Framework using dojo is like a piece of cake. In one of my previous post I discuss how to create filtering select and populate that using dojo and ajax.
Here I am going to discuss how to create auto complete with combo box. The auto complete textbox will automatically/dynamically fetch data from the database.
So lets get started.
First of all create following functions in you controller.
public function getForm()
{
if (null === $this->_form) {
$this->_form = new Zend_Form();
$this->_form->setMethod(‘get’)
->setAction(
$this->getRequest()->getBaseUrl() . ‘/test/process’
)
->addElements(array(
‘test’ => array(‘type’ => ‘text’, ‘options’ => array(
‘filters’ => array(‘StringTrim’),
‘dojoType’ => array(‘dijit.form.ComboBox’),
’store’ => ‘testStore’,
‘autoComplete’ => ‘false’,
‘hasDownArrow’ => ‘true’,
‘label’ => ‘Your input:’,
)),
‘go’ => array(‘type’ => ’submit’,
‘options’ => array(‘label’ => ‘Go!’))
));
}
return $this->_form;
}
In the above code define a function and create a form adding it a combo box.
The important attributes of the combo box are
dojoType, store and autocomplet.
zendfan said
Hi.. Could you give us an example using jqGrid with Zend Framework… I really apreciate it… =)
Thang said
Do we need to revise
public function records()
to
public function recordsAction()
?
Faheem Abbas said
Yeah.
You are right.
Thang said
Hi Faheem Abbas
I’ve tried it and it work ok. But if I change the selected field to id like this:
$sql = ‘SELECT id FROM customer’;
$result = $db->fetchAll($sql);
$dojoData= new Zend_Dojo_Data(’id’,$result,’id’);
echo $dojoData->toJson();
then I catch an exception:
Item does not have the attribute ‘name’.
I guess the problem is about the attribute of datastore so I add sortField=”[{attrubute: 'id'}]” as below:
<div dojoType="dojox.data.QueryReadStore" jsId="accountStore" sortFields="[{attribute: 'id', descending: true}]" url="baseUrl;?>/ledger/records”>
But the problem is not solved.
Do you have experience of this error?
Thang said
Fixed. Just add to form the following line:
’searchAttr’ => ‘id’
I have another problem:
When I type into combo box, the full list appear. Type more characters and the list is still full, not reduce to match with the new word typed in the box.
Faheem Abbas said
Thang,
To achieve the functionality you are up to, you will need to use another data store.
Get the value you entered in the combo, send that to your controller/action and create your new data store using javascript. Once you create new data store then assign that data store to the combo.
Sorry for not having time these days to give you proper tested example. Lot of work this week in office.
Seth said
can you show us an example of zend framework with other ajax library like script.aculo.us ? i don’t know why but i don’t like dojo a bit
something simply like save to wishlist, add as friend, star rating, simple stuff like this so we have a base to go from, i realy appreciate it! thanks!
Javi said
Could you post entire source code please?
I m having troubles to have a good start zend dojo combo integration to improve a combo dependent.
I believe your code is newer than those i found, but i couldn’t get it works.
i believe it could be great!
Thanks a lot!
Faheem Abbas said
Sorry,it won’t be possible.
You can post your problem here.
K said
Were you able to create a Star Rating control using Zend_Dojo?
Let us know, how
mic said
excelent but i need a example complet or a example without dojo.