OnlyPHP

Yii dropDownList 下拉菜单 联动菜单

2011-05-26 09:38

Yii dropDownList 下拉菜单 联动菜单

<?php
echo CHtml::dropDownList('country_id','', array(1=>'USA',7=>'France',3=>'Japan'),
array(
    'ajax' => array(
    'type'=>'POST', //request type
    'url'=>Yii::app()->createUrl('project/dynamiccities'),
    'update'=>'#city_id', //selector to update
    'data'=>array(Yii::app()->request->csrfTokenName=>Yii::app()->request->getCsrfToken(),'country_id'=>'js $("#country_id").val()')
    //leave out the data key to pass all form values through
)));

//empty since it will be filled by the other dropdown
echo CHtml::dropDownList('city_id','', array());

?>

 

 控制器

    public function actionDynamiccities()
    {
        $data=Parts::model()->findAll('prent_id=:prent_id',
                      array(':prent_id'=>(int) $_POST['country_id']));
    
        $data=CHtml::listData($data,'id','name');
        foreach($data as $value=>$name)
        {
            echo CHtml::tag('option',
                       array('value'=>$value),CHtml::encode($name),true);
        }
    }
 

二、用CActiveForm来实现

在VIEW中实现:

    <?php echo $form->dropDownList($model,'province_id',$provinceList(这个值可以通过render传递到页面),array(
        'empty'=>'-请选择-',
        'ajax'=>array(
            //指定请求地址
            'url'=>Yii::app()->createUrl('site/dynamicCity'),
            //请求数据
            'data'=>array('pid'=>'js:this.value'),
            //操作元素
            'update'=>'#SosInfo_city_id',(注意这个update的值很容易弄错,它由两部分组成:模型+ID,模型->是指本CActiveForm所承载的model名称
        ),
    )); ?>

  <?php echo $form->dropDownList($model,'city_id',City::model()->getCityList($model->province_id),array('empty'=>'-请选择-')); ?>

-->getCityList这个函数返回array()

posted on 2013-06-23 01:52  OnlyPHP  阅读(804)  评论(0编辑  收藏  举报

导航