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

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

问题描述

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



目前在yii中实现这个问题。我有这个_form文件
,其中应该包括这3个下拉列表。我有一个部分代码,但我不知道如何使这项工作出来



这是从控制器

  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 ::编码($名),TRUE);
}
}

这是从_form视图文件

 < 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'=>数组

'type'=>'POST',
'url'=> CController :: createUrl('wsmembersdetails / dynamicstates'),
'update'=>'#StateID',



);
echo $ form-> dropDownList('StateID','',array());
?>
< / div>

我将上述代码更改为此一个

 < 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'),
数组(
'ajax'=>数组(
'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'),
数组(
'ajax'=>数组(
'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'); ?>


解决方案

这个问题解决了,它在yii wiki docs


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天全站免登陆