我是Zend Framework的新手,我正在尝试使用Zend Form制作一个Form。在这个Form中,我想要国家、州和城市的级联下拉列表。我已经从数据库中填写了国家的下拉列表,但我不知道如何通过获取国家id来填写该州的下拉列表。
提前谢谢。
您必须设置这样的关联数组:
$tCountries = array(
1 => "France",
2 => "USA"
);
$element = new Zend_Form_Element_Select();
$element->addMultiOptions($tCountries);
您可以在这里找到更多信息:http://framework.zend.com/manual/en/zend.form.standardElements.html#zend.form.standardElements.select
要动态加载国家/地区,您应该像以前那样使用它:
我以前用标准php和jquery 做过这件事
这里与Zend_Form
无关。您应该有一个JSON
服务(例如),它将接受CountryID
并返回所有可用状态。
为此,您应该在<head>
部分中连接jQuery
库。
$this->view->headScript()->appendFile('/js/jquery-1.7.3.js'); // In your Controller
将jQuery
脚本添加到template
中,它将处理以下所有事件:
$_form->inlineScript()->appendScript(' // my raw JS here');
或
$_form->inlineScript()->appendFile('js/dynamicCountry.js'); // if you want to keep it in separate file
现在,除了分配正确的元素ID
、classes
或names
之外,与Zend_From
无关。
对于你的service
,你可以做smth。相像的假设URL http://localhost/mysite/service/states/country/AT
/**
* In your Service controller disable rendering for your layout and views
*/
public function preDispatch() {
$this->_helper->layout()->disableLayout(); // if you have `layout` enabled
$this->_helper->viewRenderer->setNoRender(true);
}
而且。。。欢迎
public function statesAction() {
$request = $this->getRequest();
/**
* Country code transmitted through the parameter
* @var string
*/
$country = $request->getParam('country');
// you are free to do whatever you want and return JSON (for e.g.)
}