2013-10-28 2 views
0

cakePHP에 대한 질문이 있습니다. 내보기에 두 개의 드롭 다운 목록을 만듭니다. 사용자가 하나의 목록에서 값을 변경하면 두 번째 값을 변경해야합니다. 현재이 작업은 다음과 같습니다. 사용자가 목록 상자에서 하나를 선택하면 클릭 이벤트가 발생합니다. 이것은 내 컨트롤러에서 함수를 호출하는 jQuery ajax 함수를 시작합니다. 이 모든게 잘 작동하지만 비동기 적으로 (또는 볼 때) 내 컨트롤을 어떻게 다시 렌더링합니까? 나는 json 배열을 serialize하고 자바 스크립트에서 컨트롤을 다시 만들 수 있지만 더 많은 "CakePHP"방법이 있어야하는 것처럼 보입니다. 렌더링이란 무엇을위한 것이 아닌가요? 어떤 도움이라도 좋을 것입니다.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- > 요청 -> 데이터 [ '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  

} 
+0

이에 대한 정확한 목록을 표시하도록하여 사용자가 첫 번째 드롭 다운 목록을 변경할 때마다 페이지를 다시로드 할 수 두 번째 또는 cakePHP가 새 컨트롤 만 표시하는 페이지로 ajax 호출에 응답 한 다음 ajax 호출이 반환되면 이전 컨트롤을 CakePHP가 응답 한 것으로 바꿉니다. – Kai

답변

1
  • 는 JS 이벤트는 select 태그에 해고
  • 당신은 당신의 양식을 구축하기 위해 양식 도우미를 사용할 수 있습니다 클릭하지 변화입니다.
  • 주의 cakephp 방식대로 이름을 지어줍니다. 코드이기 때문에

조금 내가 다른 간단한 예제를 만들 것 혼동 :

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을

<?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?/추가 :

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 :

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 :

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