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

高洛峰
Release: 2016-12-23 16:50:09
Original
1376 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template