当前位置:首页 > PHP教程 > PHP常见问题

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

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

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

<?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', )) ?>

在这段代码中,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('<?php echo $this->createurl('getshops')?>', { 'srcid': $('#task-order-src-id').val() }, function(html_content) { $('#task-shop-id') .html(html_content) .find('option[value=<?php echo $model->src_shop_id?>]') .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; }


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!