[转]Yii dropDownList 下拉菜单 联动菜单

转载自: http://hi.baidu.com/xiyi2008/blog/item/327d0b22678cf3e4d7cae2ae.html

<?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);
        }
    }

posted on 2011-08-19 01:55  DavidYanXW  阅读(2427)  评论(0编辑  收藏  举报