CakePHP重新填充列表框 [英] CakePHP re-populate list box

查看:90
本文介绍了CakePHP重新填充列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于cakePHP的问题。我在我的视图中创建两个下拉列表。当用户更改一个列表中的值时,我想要第二个更改。目前,我有这样工作:当用户从列表框中选择一个点击事件触发。这将触发一个jQuery ajax函数,从我的控制器调用一个函数。这一切工作正常,但如何重新呈现我的控件,异步(或,查看)?我知道我可以只是序列化数组到json,然后在javascript中重新创建控件,但似乎应该有一个更CakePHP的方式。这不是什么渲染是为?任何帮助将是伟大的。这里是我到目前为止的代码:

I have a question about cakePHP. I create two drop down lists in my view. When the user changes the value in one list, I want the second to change. Currently, I have this working like this: An on click event fires when the user selects from list box one. This fires a jQuery ajax function that calls a function from my controller. This is all working fine, but how do I re-render my control, asynchronously (or, view)? I i know I could just serialize the array to json and then recreate the control in javascript, but there seems like there should be a more "CakePHP" way. Isn't that what render is for? Any help would be great. Here's the code I have so far:

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>';

?>

控制器函数:

public function getCurrentResp (){
$ getId = $ this-> request-> data ['roleId'];

public function 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       

}


推荐答案


  • js事件是在select标签上触发 change

  • 您可以使用表单助手建立表单。

  • 注意遵循cakephp方式命名。

  • 因为你的代码有点困惑,我会做其他简单的例子:

    Because your code is a bit confused i will make other simple example:

    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, ....)
    

    查看/ Users / add.ctp

    View/Users/add.ctp

    <?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 /添加:

    UsersController.php / add:

    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:

    CountriesController.php / get_cities:

    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_cities.ctp:

    View/Cities/get_cities.ctp :

    <?php 
        if(!empty($cities)){
            foreach ($cities as $id => $name) {
    ?>
    <option value="<?php echo $id; ?>"><?php echo $name; ?></option>
    <?php           
            }
        }
    ?>
    

    这篇关于CakePHP重新填充列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆