Heim  >  Artikel  >  php教程  >  yii实现级联下拉菜单的方法,yii实现下拉菜单

yii实现级联下拉菜单的方法,yii实现下拉菜单

WBOY
WBOYOriginal
2016-06-13 09:27:55774Durchsuche

yii实现级联下拉菜单的方法,yii实现下拉菜单

本文详细讲述了yii实现级联下拉菜单的方法,具体步骤如下:

1.模版中加入如下代码:

<&#63;php
 echo $form->dropDownList($model, 'src_type_id', OrderSrc::options(), array(
 <span style="white-space:pre"> </span>'id' => 'task-order-src-id',
 ));
 echo $form->dropDownList($model, 'src_shop_id', array(''=>'全部'), array(
 <span style="white-space:pre"> </span>'id' => 'task-shop-id',
 ))
&#63;>

在这段代码中,OrderSrc_options() 这个是先读取一个下拉菜单。调用OrderScr model中的options方法。内容如下

public static function options($hasShop = true) {
 $model = new self();
 if($hasShop) $model->hasShop();
 $models = $model->findAll();
 $array = array(''=>'全部');
 foreach($models as $model) {
 $array[$model->src_id] = $model->src_name;
 }
 return $array;
}

2.然后在模版页面中增加JS代码,实现当第一个下拉菜单变化时给第二个下拉菜单进行内容赋值。

<script type='text/javascript'>
$().ready(function(e) {
 $('#task-order-src-id').change(function(e) {
 refreshShops();
 });
 refreshShops();
 function refreshShops() {
 $.get('<&#63;php echo $this->createUrl('getShops')&#63;>', {
  'srcId': $('#task-order-src-id').val()
 }, function(html_content) {
  $('#task-shop-id')
  .html(html_content)
  .find('option[value=<&#63;php echo $model->src_shop_id&#63;>]')
   .attr('selected', 'selected');
 });
 }
});
</script>

在这段JS代码中,实现调取一个程序获取第二个下拉菜单的值(调用Controller中的actionGetShops方法),任何追加到第二个下拉菜单中。

Controller中的actionGetShops方法如下:

public function actionGetShops() {
 $srcId = $_GET['srcId'];
 $array = ThirdpartInterfaceConfig::options($srcId);
 $htmlContent = "<option value=''>全部</options>";
 foreach($array as $k=>$v) {
 $htmlContent .= "<option value='{$k}'>{$v}</option>";
 }
 echo $htmlContent;
}

yii框架中,怎利用下拉菜单进行筛选

用ajax把选取到的数据发送到controller中,获取到结果显示在view上
 

yii怎做下拉菜单,不是联动二级的,就是那种比如我想发邮件给别人,可以有个下拉菜单随便选择要发给谁的

用ajax吧,触发onchange事件提交就可以了
 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn