欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Yii2實現(xiàn)上下聯(lián)動下拉框功能的方法

 更新時間:2016年08月10日 11:56:06   作者:wjtlht928  
這篇文章主要介紹了Yii2實現(xiàn)上下聯(lián)動下拉框功能的方法,簡單分析了上下聯(lián)動下拉框功能的概念、原理與實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Yii2實現(xiàn)上下聯(lián)動下拉框功能的方法。分享給大家供大家參考,具體如下:

首先我先解釋下什么是上下聯(lián)動的下拉框

假如一個view里面有兩個select,第一個是公司名,第二個是分公司名。公司有多個,每個公司又有多個分公司,我們實現(xiàn)的就是點擊當前公司后,分公司里面顯示的事當前公司的分公司。

或者你直接理解成選擇所屬省份后,下面的select顯示的是當前省份的縣。

原理:

點擊第一個select后,執(zhí)行ajax獲取當前公司的分公司,并使用jQuery修改分公司內(nèi)容

兩個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相關內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。

相關文章

最新評論