Home php教程 PHP开发 Yii2 method to implement up-down linkage drop-down box function

Yii2 method to implement up-down linkage drop-down box function

Dec 23, 2016 pm 04:50 PM

The example in this article describes how Yii2 implements the up-and-down linkage drop-down box function. Share it with everyone for your reference, the details are as follows:

First of all, let me explain what is a drop-down box with up and down linkage

If there are two selections in a view, the first one is the company name, and the second one is the branch name. There are multiple companies, and each company has multiple branches. What we achieve is that after clicking on the current company, the branches displayed in the branches are the branches of the current company.

Or you can directly understand that after selecting the province, the selection below displays the counties of the current province.

Principle:

After clicking the first select, execute ajax to get the branch of the current company, and use jQuery to modify the branch content

The partial view code of the two selects is as follows:

<?= $form->field($model, &#39;companies_company_id&#39;)->dropDownList(
  \yii\helpers\ArrayHelper::map(\backend\models\Companies::find()->all(),&#39;company_id&#39;,&#39;company_name&#39;),
  [
    &#39;prompt&#39;=>&#39;select Company&#39;,
    &#39;onchange&#39;=>&#39;
      $.post("index.php?r=branches/lists&id=&#39;.&#39;"+$(this).val(),function(data){
        $("select#departments-branches_branch_id").html(data);
      });&#39;,
  ]
) ?>
<?= $form->field($model, &#39;branches_branch_id&#39;)->dropDownList(
  \yii\helpers\ArrayHelper::map(\backend\models\Branches::find()->all(),&#39;branch_id&#39;,&#39;branch_name&#39;),
  [
    &#39;prompt&#39;=>&#39;Select Branches&#39;,
  ]
) ?>
Copy after login

list method code :

public function actionLists($id)
{
  $countBranches = Branches::find()
    ->where([&#39;companies_company_id&#39; => $id])
    ->count();
  $branches = Branches::find()
    ->where([&#39;companies_company_id&#39; => $id])
    ->all();
  if ($countBranches > 0) {
    foreach ($branches as $branche) {
      echo "<option value=&#39;" . $branche->branch_id . "&#39;>" . $branche->branch_name . "</option>";
    }
  } else {
    echo "<option>-</option>";
  }
}
Copy after login

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

For more related articles on how Yii2 implements the up-down linkage drop-down box function, please pay attention to the PHP Chinese website!

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24