CakePHP Js Helper - 动态更新 3 个下拉菜单

CakePHP Js Helper - Update 3 Dropdown menus dynamically

我正在使用 CakePHP 2.6.1

我有一个 cakephp 表单来处理对这 3 个下拉菜单的访问: 地点->设施->部门

我希望它们是动态填充的,所以我遵循了本教程http://marnienickelson.com/2014/10/11/dynamic-dropdowns-with-cakephp-2-x/

除一个小问题外,它运行良好。如果我更改 "location","facility" 下拉菜单会正确填写,但 "department" 菜单保持空白...

我的AccessesController.php

public function add() {
    if ($this->request->is('post')) {
        $this->Access->create();
        if ($this->Access->save($this->request->data)) {
            $this->Session->setFlash(__('The access has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The access could not be saved. Please, try again.'));
        }
    }
    $titles = $this->Access->Title->find('list');
    $locations = $this->Access->Facility->Location->find('list');
    $systems = $this->Access->System->find('list');
    $this->set(compact('titles', 'locations', 'facilities', 'departments', 'systems'));
}

我的get_by_location.ctp(我有一个名为get_by_facility.ctp的相同文件)

<?php foreach ($facilities as $key => $value): ?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php endforeach; ?>

而在我的 add.ctp

结束时
<?php
$this->Js->get('#AccessLocationId')->event('change', 
$this->Js->request(array(
    'controller'=>'facilities',
    'action'=>'getByLocation'
    ), array(
    'update'=>'#AccessFacilityId',
    'async' => true,
    'method' => 'post',
    'dataExpression'=>true,
    'data'=> $this->Js->serializeForm(array(
    'isForm' => true,
    'inline' => true
    ))
    ))
);

$this->Js->get('#AccessFacilityId')->event('change', 
$this->Js->request(array(
    'controller'=>'departments',
    'action'=>'getByFacility'
    ), array(
    'update'=>'#AccessDepartmentId',
    'async' => true,
    'method' => 'post',
    'dataExpression'=>true,
    'data'=> $this->Js->serializeForm(array(
    'isForm' => true,
    'inline' => true
    ))
    ))
);

?>

我知道第二个事件'change' 未被识别,这就是为什么我的第三个下拉列表保持空白的原因...那么还有其他事件 'change' 吗?或者我可以将这两个 ajax 请求合二为一吗?

这个博客对我帮助很大Euromarks blog

我刚刚更改了我的 get_by_location.ctp 文件:

    <?php
if (!empty($facilities)) {
    echo '<option value="">' . __('pleaseSelect') . '</option>';
    foreach ($facilities as $k => $v) {
        echo '<option value="' . $k . '">' . h($v) . '</option>';
    }
} else {
    echo '<option value="0">' . __('noOptionAvailable') . '</option>';
}

因此,如果第一个下拉列表发生变化,第二个将显示 "please select"。