2012-07-29 210 views
0

我是Zend框架的新手,我試圖用Zend Form製作表單。在這種形式下,我想要國家,州和城市的級聯下降。我已經從數據庫中填充了國家的下拉菜單,但是我不知道如何通過獲取國家/地區ID來填寫州下拉菜單。在zend框架中級聯下拉

在此先感謝。

回答

-2

動態地加載國家,你應該使用它,你所做的一切:

我以前用標準的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 

在這裏,你是沒時間跟Zend_From盡除分配正確元件ID's,classesnames

而對於你的service你可以做水手。類似。假設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.) 
} 
+0

謝謝。這工作。 – Pankaj 2012-07-30 09:02:29

+0

很高興幫助!不用謝 ! – 2012-07-30 11:23:49

0

你必須設置一個關聯數組是這樣的:

$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

+0

感謝回答,但我已經做到了這一點。我填充了國家的下拉列表,其中包含來自數據庫中數據庫的值和密鑰。這不是我的問題。我的問題是,當用戶選擇國家,然後國家的下拉菜單應該自動填充該國家的可用狀態。我以前用標準的php和jquery完成了這個。但在這裏我想在Zend_Form中完成。 – Pankaj 2012-07-29 13:07:18

+0

當你收到表格時你會怎麼做? – 2012-07-29 13:09:45

+0

當表格加載時,國家的下拉菜單中會有數據庫中的所有國家名稱以及其他狀態下的下拉菜單,城市現在不會有任何值。當用戶選擇國家時,我希望州的下拉菜單應該填入該國的州和下拉菜單。 – Pankaj 2012-07-29 13:15:47