我有带有添加操作的控制器。
public function add() {
$this->layout = 'manage';
$this->set($this->Restaurant->fetchRelatedData());
if ($this->request->is('post')) {
$this->Restaurant->create();
if ($this->Restaurant->save($this->request->data)) {
$this->Session->setFlash('ok!');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Error!');
}
}
}
使用 Form 和 Js 助手创建的此操作的视图:
echo $this->Form->create('Restaurant');
// some fields
echo $this->Form->input('district_id', array('label' => 'District'));
echo $this->Form->input('street_id', array('label' => 'Street'));
// other fields
echo $this->Form->end(array('label' => 'Add'));
$this->Js->get('#RestaurantDistrictId')->event('change',
$this->Js->request(array(
'controller'=>'streets',
'action'=>'getByDistrict'
), array(
'update'=>'#RestaurantStreetId',
'async' => true,
'method' => 'post',
'dataExpression'=>true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
Js 助手显示所选区域中的街道列表。
StreetsController -> getByDistrict 操作:
public function getByDistrict(){
$district_id = $this->request->data['Restaurant']['district_id'];
$streets = $this->Street->find('list', array(
'conditions' => array('Street.district_id' => $district_id),
'fields' => array('street'),
'order' => array('Street'),
'recursive' => -1,
));
$this->set('streets', $streets);
$this->layout = 'ajax';
}
在我向此操作添加管理前缀之前,一切正常。如果 action 被命名为public function add() - 一切正常。如果 action 被命名为 *public function admin_add()* – Js helper 停止更新街道列表以改变地区。