网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的范文:

 

标题 Yii2实现上下联动下拉框功能的方法
范文
    本文实例讲述了Yii2实现上下联动下拉框功能的方法。分享给大家供大家参考,具体如下:
    首先我先解释下什么是上下联动的下拉框
    假如一个view里面有两个select,第一个是公司名,第二个是分公司名。公司有多个,每个公司又有多个分公司,我们实现的就是点击当前公司后,分公司里面显示的事当前公司的分公司。
    或者你直接理解成选择所属省份后,下面的select显示的是当前省份的县。
    原理:
    点击第一个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框架的PHP程序设计有所帮助。
随便看

 

在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/19 6:48:23