我有一个关于 cakePHP 的问题。我在我的视图中创建了两个下拉列表。当用户更改一个列表中的值时,我希望第二个更改。目前,我的工作方式如下:当用户从列表框一中选择时,会触发点击事件。这会触发一个 jQuery ajax 函数,该函数从我的控制器中调用一个函数。这一切都很好,但是我如何异步(或查看)重新渲染我的控件?我知道我可以将数组序列化为 json,然后在 javascript 中重新创建控件,但似乎应该有一种更“CakePHP”的方式。这不就是渲染的目的吗?任何帮助都会很棒。这是我到目前为止的代码:
jQuery:
function changeRole(getId){
$.ajax({
type: 'POST',
url: 'ResponsibilitiesRoles/getCurrentResp',
data: { roleId: getId },
cache: false,
dataType: 'HTML',
beforeSend: function(){
},
success: function (html){
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
看法:
<?php
echo 'Roles:';
echo'<select name="myOptions" multiple="multiple">';
foreach ($rolesResponsibility as $role) {
echo' <option onclick="changeRole(this.value);" value="'; echo $role["ResponsibilitiesRole"]["role_id"]; echo '">'; echo $role["r"]["role_name"]; echo '</option>';
}
echo '</select>';
echo 'Responsbility:';
echo'<select name="myOptionsResp" multiple="multiple">';
foreach ($respResponsibility as $responsibility) {
echo' <option value="'; echo $responsibility["responsibility"]["id"]; echo '">'; echo $responsibility["responsibility"]["responsibility_name"]; echo '</option>';
}
echo '</select>';
?>
控制器功能:
公共函数 getCurrentResp(){ $getId = $this->request->data['roleId'];
$responsibilityResp = $this->ResponsibilitiesRole->find('all',
array("fields" => array('role.role_name','ResponsibilitiesRole.role_id','responsibility.*'),'joins' => array(
array(
'table' => 'responsibilities',
'alias' => 'responsibility',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array('ResponsibilitiesRole.responsibility_id = responsibility.id')
),
array(
'table' => 'roles',
'alias' => 'role',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array('ResponsibilitiesRole.role_id = role.id')
)
),
'conditions' => array ('ResponsibilitiesRole.role_id' => $getId),
));
$this->set('respResponsibility', $responsibilityResp);
//do something here to cause the control to be rendered, without have to refresh the whole page
}