源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Yii2实现上下联动下拉框功能的方法

  • 时间:2020-01-29 20:15 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Yii2实现上下联动下拉框功能的方法
本文实例讲述了Yii2实现上下联动下拉框功能的方法。分享给大家供大家参考,具体如下: 首先我先解释下[b]什么是上下联动的下拉框[/b] 假如一个view里面有两个select,第一个是公司名,第二个是分公司名。公司有多个,每个公司又有多个分公司,我们实现的就是点击当前公司后,分公司里面显示的事当前公司的分公司。 或者你直接理解成选择所属省份后,下面的select显示的是当前省份的县。 [b]原理:[/b] 点击第一个select后,执行ajax获取当前公司的分公司,并使用jQuery修改分公司内容 两个select的部分视图代码如下:
<?= $form->field($model, 'companies_company_id')->dropDownList(
  \yii\helpers\ArrayHelper::map(\backend\models\Companies::find()->all(),'company_id','company_name'),
  [
    'prompt'=>'select Company',
    'onchange'=>'
      $.post("index.php?r=branches/lists&id='.'"+$(this).val(),function(data){
        $("select#departments-branches_branch_id").html(data);
      });',
  ]
) ?>
<?= $form->field($model, 'branches_branch_id')->dropDownList(
  \yii\helpers\ArrayHelper::map(\backend\models\Branches::find()->all(),'branch_id','branch_name'),
  [
    'prompt'=>'Select Branches',
  ]
) ?>

list方法代码:
public function actionLists($id)
{
  $countBranches = Branches::find()
    ->where(['companies_company_id' => $id])
    ->count();
  $branches = Branches::find()
    ->where(['companies_company_id' => $id])
    ->all();
  if ($countBranches > 0) {
    foreach ($branches as $branche) {
      echo "<option value='" . $branche->branch_id . "'>" . $branche->branch_name . "</option>";
    }
  } else {
    echo "<option>-</option>";
  }
}

更多关于Yii相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/386.htm]Yii框架入门及常用技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/155.htm]php优秀开发框架总结[/url]》、《[url=http://www.1sucai.cn/Special/26.htm]smarty模板入门基础教程[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部