Home Backend Development PHP Tutorial yii实现级联下拉菜单的方法_php实例

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

Jun 07, 2016 pm 05:16 PM
yii Drop-down menu cascade

本文详细讲述了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;>

Copy after login

在这段代码中,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;
}

Copy after login

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>

Copy after login

在这段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;
}
Copy after login
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make drop-down menu in WPS table How to make drop-down menu in WPS table Mar 21, 2024 pm 01:31 PM

How to make drop-down menu in WPS table

How to add artistic page borders in Microsoft Word How to add artistic page borders in Microsoft Word Apr 27, 2023 pm 08:25 PM

How to add artistic page borders in Microsoft Word

How to print a Word document without comments How to print a Word document without comments Apr 18, 2023 pm 02:19 PM

How to print a Word document without comments

5 Ways (and Fixes) to Adjust Your Screen for Monitoring on Windows 11 5 Ways (and Fixes) to Adjust Your Screen for Monitoring on Windows 11 Apr 14, 2023 pm 03:28 PM

5 Ways (and Fixes) to Adjust Your Screen for Monitoring on Windows 11

How to set image transparency in Google Slides? How to set image transparency in Google Slides? Apr 25, 2023 pm 06:52 PM

How to set image transparency in Google Slides?

Implement the drop-down menu effect in WeChat applet Implement the drop-down menu effect in WeChat applet Nov 21, 2023 pm 03:03 PM

Implement the drop-down menu effect in WeChat applet

How to use system-wide live subtitles on Windows 11 How to use system-wide live subtitles on Windows 11 May 02, 2023 pm 01:19 PM

How to use system-wide live subtitles on Windows 11

How to implement drop-down menu using Vue? How to implement drop-down menu using Vue? Jun 26, 2023 am 12:17 AM

How to implement drop-down menu using Vue?

See all articles