首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP重新填充列表框

CakePHP重新填充列表框
EN

Stack Overflow用户
提问于 2013-10-28 18:55:36
回答 1查看 1.1K关注 0票数 0

我有一个关于cakePHP的问题。我在视图中创建了两个下拉列表。当用户更改一个列表中的值时,我希望第二个列表更改。目前,我的工作方式如下:当用户从列表框1中选择时,单击事件就会触发。这将触发一个jQuery ajax函数,该函数从我的控制器调用一个函数。这一切都很好,但是如何异步地重新呈现我的控件(或视图)?我知道我可以将数组序列化为json,然后在javascript中重新创建控件,但是似乎应该有一种更"CakePHP“的方法。这不是渲染的目的吗?任何帮助都会很好。下面是我到目前为止掌握的代码:

jQuery:

代码语言:javascript
复制
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) {

    }

});

查看:

代码语言:javascript
复制
<?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‘;

代码语言:javascript
复制
$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       

}
EN

回答 1

Stack Overflow用户

发布于 2013-10-28 20:45:51

  • js事件是在select标记上触发的change,而不是单击
  • 您可以使用“表单助手”生成窗体。
  • 注意按照cakephp的方式命名事物。

由于您的代码有点混乱,我将做另一个简单的示例:

代码语言:javascript
复制
Country hasMany City
User belongsTo Country
User belongsTo City

ModelName/TableName (fields)
Country/countries (id, name, ....) 
City/cities (id, country_id, name, ....)
User/users (id, country_id, city_id, name, ....)

查看/用户/add.ctp

代码语言:javascript
复制
<?php
    echo $this->Form->create('User');
    echo $this->Form->input('country_id');
    echo $this->Form->input('city_id');
    echo $this->Form->input('name');
    echo $this->Form->end('Submit');

    $this->Js->get('#UserCountryId')->event('change',
        $this->Js->request(
            array('controller' => 'countries', 'action' => 'get_cities'),
                array(
                    'update' => '#UserCityId',
                    'async' => true,
                    'type' => 'json',
                    'dataExpression' => true,
                    'evalScripts' => true,
                    'data' => $this->Js->serializeForm(array('isForm' => false, 'inline' => true)),
            )
        )
    );
    echo $this->Js->writeBuffer();

?>

UsersController.php / add:

代码语言:javascript
复制
public function add(){
    ...
    ...
    // populate selects with options
    $this->set('countries', $this->User->Country->find('list'));
    $this->set('cities', $this->User->City->find('list'));
}

CountriesController.php / get_cities:

代码语言:javascript
复制
public function get_cities(){
    Configure::write('debug', 0);
    $cities = array();
    if(isset($this->request->query['data']['User']['country_id'])){
        $cities = $this->Country->City->find('list', array(
                  'conditions' => array('City.country_id' => $this->request->query['data']['User']['country_id'])
        ));
    }
    $this->set('cities', $cities);
}

查看/城市/获取城市。get:

代码语言:javascript
复制
<?php 
    if(!empty($cities)){
        foreach ($cities as $id => $name) {
?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php           
        }
    }
?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19642872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档