本文实例讲述了Yii实现的多级联动下拉菜单。分享给大家供大家参考,具体如下:
1. 视图文件
1
2
3
4
5
6
7
8
9
10
11
|
<?php echo CHtml::activeDropDownList( $model , 'zmg_id' ,MemGroup::model()->getMemGroup(), array ( 'class' => 's_ipt w_120' , 'empty' => '请选择会员组' , 'ajax' => array ( 'type' => 'GET' , 'url' =>CController::createUrl( 'cmpTemplates/getMemType' ), 'update' => '#CmpTemplates_zmg_ids' , 'data' => array ( 'mid' => "js:this.value" ) ), ))?> <?php echo $form ->dropDownList( $model , 'zmg_ids' , array (), array ( 'class' => 's_ipt w_120' , 'empty' => '选择会员等级' ))?> |
2. 控制器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * 获取会员组,对应的会员等级,用于下拉菜单 */ public function actionGetMemType( $mid =0) { $criteria = new CDbCriteria; $criteria ->compare( 'zmg_id' , $mid ); $memType = MemType::model()->findAll( $criteria ); $name = '选择会员等级' ; echo CHtml::tag( 'option' , array ( 'value' =>0), $name , true); foreach ( $memType as $val ) { echo CHtml::tag( 'option' , array ( 'value' => $val ->zmt_id),CHtml::encode( $val ->zmt_title),true); } } |
3. 模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/* * 取会员组信息 */ public function getMemGroup( $type =null){ if ( $type ==null){ $criteria = new CDbCriteria; $criteria ->compare( 'type' , '1' ); $memGroup = MemGroup::model()->findAll( $criteria ); return CHtml::listData( $memGroup , 'zmg_id' , 'zmg_title' ); } else { $level = $this ->getMemGroup(); if ( array_key_exists ( $type , $level )){ return $level [ $type ]; } } } |
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。