Home  >  Article  >  Backend Development  >  About the multi-level linkage drop-down menu implemented by Yii

About the multi-level linkage drop-down menu implemented by Yii

不言
不言Original
2018-06-15 16:52:081951browse

This article mainly introduces the multi-level linkage drop-down menu implemented by Yii, including the relevant implementation code of views, models and controllers, and involves related operation skills such as database query, array traversal and data display based on Yii. Friends in need You can refer to the example below

This article describes the multi-level linkage drop-down menu implemented by Yii. Share it with everyone for your reference, the details are as follows:

1. View file

<?php echo CHtml::activeDropDownList($model,&#39;zmg_id&#39;,MemGroup::model()->getMemGroup(),array(
    &#39;class&#39;=>&#39;s_ipt w_120&#39;,
    &#39;empty&#39;=>&#39;请选择会员组&#39;,
    &#39;ajax&#39; =>array(
          &#39;type&#39;=>&#39;GET&#39;,
          &#39;url&#39;=>CController::createUrl(&#39;cmpTemplates/getMemType&#39;),
          &#39;update&#39;=>&#39;#CmpTemplates_zmg_ids&#39;,
          &#39;data&#39;=>array(&#39;mid&#39;=>"js:this.value")
          ),
    ))?>
<?php echo $form->dropDownList($model,&#39;zmg_ids&#39;,array(),array(&#39;class&#39;=>&#39;s_ipt w_120&#39;,&#39;empty&#39;=>&#39;选择会员等级&#39;))?>

##2. Controller

/**
 * 获取会员组,对应的会员等级,用于下拉菜单
 */
public function actionGetMemType($mid=0)
{
  $criteria=new CDbCriteria;
  $criteria->compare(&#39;zmg_id&#39;,$mid);
  $memType = MemType::model()->findAll($criteria);
  $name = &#39;选择会员等级&#39;;
  echo CHtml::tag(&#39;option&#39;, array(&#39;value&#39;=>0), $name, true);
  foreach($memType as $val) {
    echo CHtml::tag(&#39;option&#39;, array(&#39;value&#39;=>$val->zmt_id),CHtml::encode($val->zmt_title),true);
  }
}

3. Model

/*
* 取会员组信息
*/
public function getMemGroup($type=null){
  if($type==null){
    $criteria=new CDbCriteria;
    $criteria->compare(&#39;type&#39;,&#39;1&#39;);
    $memGroup = MemGroup::model()->findAll($criteria);
    return CHtml::listData($memGroup,&#39;zmg_id&#39;,&#39;zmg_title&#39;);
  }else{
    $level = $this->getMemGroup();
    if(array_key_exists($type,$level)){
      return $level[$type];
    }
  }
}

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

yii2 implements the code about "previous page, next page" in the page

How to solve Yii2 prevention rules and restrictions for visitors and users

The above is the detailed content of About the multi-level linkage drop-down menu implemented by Yii. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn