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

2、用CActiveForm來實現php

在VIEW中實現:ajax

    <?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'=>'-請選擇-')); ?>app

-->getCityList這個函數返回array()函數

相關文章
相關標籤/搜索