加载中...

Yii - 连动下拉菜单,可多级哦


  1. 视图文件
    1. <?php echo CHtml::activeDropDownList($model,'zmg_id',MemGroup::model()->getMemGroup(),array(
    2. 'class'=>'s_ipt w_120',
    3. 'empty'=>'请选择会员组',
    4. 'ajax' =>array(
    5. 'type'=>'GET',
    6. 'url'=>CController::createUrl('cmpTemplates/getMemType'),
    7. 'update'=>'#CmpTemplates_zmg_ids',
    8. 'data'=>array('mid'=>"js:this.value")
    9. ),
    10. ))?>
    11. <?php echo $form->dropDownList($model,'zmg_ids',array(),array('class'=>'s_ipt w_120','empty'=>'选择会员等级'))?>

  2. 控制器
    1. /**
    2. * 获取会员组,对应的会员等级,用于下拉菜单
    3. */
    4. public function actionGetMemType($mid=0)
    5. {
    6. $criteria=new CDbCriteria;
    7. $criteria->compare('zmg_id',$mid);
    8. $memType = MemType::model()->findAll($criteria);
    9. $name = '选择会员等级';
    10. echo CHtml::tag('option', array('value'=>0), $name, true);
    11. foreach($memType as $val) {
    12. echo CHtml::tag('option', array('value'=>$val->zmt_id),CHtml::encode($val->zmt_title),true);
    13. }
    14. }

  3. 模型
    1. /*
    2. * 取会员组信息
    3. */
    4. public function getMemGroup($type=null){
    5. if($type==null){
    6. $criteria=new CDbCriteria;
    7. $criteria->compare('type','1');
    8. $memGroup = MemGroup::model()->findAll($criteria);
    9. return CHtml::listData($memGroup,'zmg_id','zmg_title');
    10. }else{
    11. $level = $this->getMemGroup();
    12. if(array_key_exists($type,$level)){
    13. return $level[$type];
    14. }
    15. }
    16. }


还没有评论.