How to populate a select with Jquery View Helper and JSON (ZEND)
I know that zend framework have some easy ways to do this, but i didn't found a good answer or tutorial to do things according to zend.
Some answers show how to do without JSON, others without Jquery View Helper, and others without using zend framework.
The thing is too simple We have 2 Selects: group and subgroup When I change group, the subgroup is populated.
This is the action:
public function getSubgroupAction(){
$model = new MySubgroupModel();
$id = $this->getRequest()->getParam('id');
$where = "id = $id";
$data = $model->fetchAll($where)->toArray();
$json = Zend_Json::encode($data);
return $json;
}
And this is the form
public function init(){
$model = new MyGroupModel();
$options = $model->fetchAll();
// $options receive a query with id and name
$group = new Zend_Form_Element_Select('group');
$group->setLabel('Chose Group')
->addMultiOptions($options)
->setRequired();
$subgroup = new Zend_Form_Element_Select('subgroup');
$subgroup->setLabel('Chose Subgroup')
->setRequired();
}
I want to know where to put on form the request to 'getSubgroupAction' or what i have to do using jquery 开发者_如何学Goview helper to get the json response from the action.
Note: This is not a working example.
I antecipate my thanks for the attention and the help. tkzalot.
First in your Controller in init change your context to json.
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('getSubgroup', 'json')
->setAutoJsonSerialization(false) // or true to automatically do it for you
->initContext();
If your only going to always render json then disable the layout.
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
In your getSubgroup action after you hit you DB and get the data you want encode the json and echo.
echo Zend_Json_Encoder::encode($result);
If you scroll to this action json should be displayed in your browser.
With ajax you can always go and get the json response to display where ever you want. There are other ways of doing this instead of using a controller to send the json response but this is probably the easiest way to get your link working.
精彩评论