蛋糕中的F ormHelper ::输入方法将呈现与optgroups一个选择标记,如果选项是正确的,例如
echo $form->input('county');
只要是在视图中提供一个变量名为$各县其中包含下列格式的数据:
$counties = array(
'Country Name 1' => array(
'county_1_id' => 'County 1 Name',
'county_2_id' => 'County 2 Name',
'county_3_id' => 'County 3 Name',
),
'Country Name 2' => array(
'county_4_id' => 'County 4 Name',
'county_5_id' => 'County 5 Name',
'county_6_id' => 'County 6 Name',
),
);
所以,在你的控制,这样做:
$this->set('counties', ClassRegistry::init('Country')->getCountiesByCountry());
,并在你的国家模式,这样做:
function getCountiesByCountry() {
$countries = $this->find('all', array('contain' => array('County')));
$return = array();
foreach ($countries as $country) {
foreach ($country['County'] as $county) {
$return[$country['Country']['name']][$county['id']] = $county['name'];
}
}
return $return;
}
哇..这是可爱的..谢谢了很多仍然 – leo 2009-12-16 12:10:38
非常很有帮助,谢谢。 – 2013-05-19 11:55:21