Yii的依赖下拉框 [英] dependent dropdown box with Yii

查看:35
本文介绍了Yii的依赖下拉框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个国家、州、城市表,我需要 3 个下拉菜单其中,如果我选择一个国家,例如美国,州下拉菜单将自动只显示美国下的州,接下来,如果我选择一个州,例如加利福尼亚,城市下拉菜单将只显示美国加利福尼亚以下的城市

我目前在 yii 中实现这个有问题.我有这个 _form 文件应包括这 3 个下拉列表.我有部分代码,但我不知道如何解决

这是来自控制器

公共函数 actionDynamicstates(){$sql = "SELECT StateName FROM gg_t_worldareasstates "."WHERE CountryID = :countryid";$command = Yii::app()->createCommand($sql);$command->bindValue(':countryid', $_POST['CountryID'], PDO::PARAM_INT);$data = $command->execute();$data = CHtml::listData($data,'StateID','StateName');foreach($data as $value=>$name){echo CHtml::tag('选项',array('value'=>$value),CHtml::encode($name),true);}}

这是来自_form视图文件

<?php$country = 新的 CDbCriteria;$country->order = 'CountryName ASC';echo $form->dropDownList($model, 'CountryID',CHtml::listData(Worldareascountries::model()->findAll($country),'国家ID',大批('阿贾克斯' =>大批('类型' =>'邮政','网址' =>CController::createUrl('wsmembersdetails/dynamicstates'),'更新' =>'#StateID',))));echo $form->dropDownList('StateID','', array());?>

我把上面的那些代码改成了这个

 

<?php echo $form->labelEx($model,'Country');?><?php$country = 新的 CDbCriteria;$country->order = 'CountryName ASC';?><?phpecho $form->dropDownList($model,'CountryID',CHtml::listData(Worldareascountries::model()->findAll($country),'CountryID','CountryName'),大批('阿贾克斯' =>大批('类型' =>'邮政','网址' =>CController::createUrl('wsmembersdetails/dynamicstates'),'更新' =>#StateID")));?><?php echo $form->error($model,'CountryID');?>

<div class="row"><?php echo $form->labelEx($model,'State');?><?php$state = 新的 CDbCriteria;$state->order = 'StateName ASC';?><?phpecho $form->dropDownList($model,'StateID',CHtml::listData(Worldareasstates::model()->findAll($state),'StateID','StateName'),大批('阿贾克斯' =>大批('类型' =>'邮政','网址' =>CController::createUrl('wsmembersdetails/dynamiccities'),'更新' =>'#CityID')));?><?php echo $form->error($model,'StateID');?>

<div class="row"><?php echo $form->labelEx($model,'CityID');?><?php echo $form->dropDownList($model,'CityID','',array());?><?php echo $form->error($model,'CityID');?>

解决方案

这个问题已经解决,在 yii wiki 文档上

i have a countries, states, cities tables , and I need 3 dropdown menus wherein, if i select a country like e.g United States, the states dropdown menu will automatically show only the states that are under United States, and next, if I select a State like e.g California, the Cities drop down menu will only show off the Cities under California

am currently having problem implementing this in yii. I have this _form file where these 3 dropdown should be included. I have a partial code, but I can't figure out how to make this work out

this is from the Controller

public function actionDynamicstates()
{
  $sql = "SELECT StateName FROM gg_t_worldareasstates ".
         "WHERE CountryID = :countryid";
  $command = Yii::app()->createCommand($sql);
  $command->bindValue(':countryid', $_POST['CountryID'], PDO::PARAM_INT);
  $data = $command->execute();

  $data = CHtml::listData($data,'StateID','StateName');
  foreach($data as $value=>$name)
  {
    echo CHtml::tag('option',
    array('value'=>$value),CHtml::encode($name),true);
  }
}

this is from the _form view file

<div class="row">
<?php 
$country = new CDbCriteria; 
$country->order = 'CountryName ASC';
echo $form->dropDownList($model, 'CountryID', 
  CHtml::listData
  (
    Worldareascountries::model()->findAll($country),
    'CountryID',
    array
    (
      'ajax' => array
      (
        'type' => 'POST',
        'url' => CController::createUrl('wsmembersdetails/dynamicstates'),
        'update' => '#StateID',
      )
    )
  )
);
echo $form->dropDownList('StateID','', array());
?>
</div>

I changed that those codes above into this one

    <div class="row">
    <?php echo $form->labelEx($model,'Country'); ?>
    <?php 
          $country = new CDbCriteria; 
          $country->order = 'CountryName ASC';
    ?>
    <?php 
          echo $form->dropDownList($model,'CountryID',CHtml::listData(Worldareascountries::model()->findAll($country),'CountryID','CountryName'),
                    array(
                        'ajax' => array(
                        'type' => 'POST',
                        'url' => CController::createUrl('wsmembersdetails/dynamicstates'),
                        'update' => "#StateID"
                    )       
              )
          );
    ?>
    <?php echo $form->error($model,'CountryID'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'State'); ?>
    <?php 
          $state = new CDbCriteria;
          $state->order = 'StateName ASC';
    ?>
    <?php 
          echo $form->dropDownList($model,'StateID',CHtml::listData(Worldareasstates::model()->findAll($state),'StateID','StateName'),
                    array(
                        'ajax' => array(
                        'type' => 'POST',
                        'url' => CController::createUrl('wsmembersdetails/dynamiccities'),
                        'update' => '#CityID'
                    )   
                )
          );
    ?>
    <?php echo $form->error($model,'StateID'); ?>
</div>


<div class="row">

    <?php echo $form->labelEx($model,'CityID'); ?>
     <?php echo $form->dropDownList($model,'CityID','',array());?>
    <?php echo $form->error($model,'CityID'); ?>

解决方案

this problem was solved, it's on the yii wiki docs

这篇关于Yii的依赖下拉框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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