Methods of implementing the cascading drop-down menu in Yii

Source: Internet
Author: User

1. Add the following code to the template:

?
12345678 <?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‘, ))?>

In this code, ordersrc_options () reads a drop-down menu first. Call the options method in the ORDERSCR model. The contents are as follows

?
12345678910 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. Then add the JS code to the template page to make the content assignment to the second drop-down menu when the first drop-down menu changes.

?
123456789101112131415161718 <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>

In this JS code, the implementation calls a program to get the value of the second drop-down menu (call the Actiongetshops method in the controller), any append to the second drop-down menu.

The Actiongetshops method in the controller is as follows:

?
123456789 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;}

Methods of implementing the cascading drop-down menu in Yii

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.