Menus

Tuesday 28 March 2017

Yii2 Dependent Drop Down Lists


Creating a Dependent Dropdown From in Yii2





Company drop down list change to display corresponding branches display on branches drop down  list box.

First create table companies, branches and department

companies
company_id
company_name

branches
branch_id
companies_company_id
branch_name

departments
department_id
branches_branch_id
companies_company_id
department_name
status

* companies_company_id  set relation to company_id
* branches_branch_id set relation to branch_id




Create companies ,branches and departments crud operations .

Department create view _form.php code


<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use backend\modules\test\models\Companies;
use backend\modules\test\models\Branches;

?>

<div class="departments-form">

    <?php $form = ActiveForm::begin(); ?>

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

    <?= $form->field($model, 'department_name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'status')->dropDownList([ 'Active' => 'Active', 'Inactive' => 'Inactive', ], ['prompt' => '']) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>


Note :
test/branches/lists  - create action lists in branches controller.
departments-branches_branch_id - id of branch drop down list controller.
Send company id send to branches controller lists action

The BrachesController.php



<?php

namespace backend\modules\test\controllers;

use Yii;
use backend\modules\test\models\Branches;
use backend\modules\test\models\BranchesSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;


/**
 * BranchesController implements the CRUD actions for Branches model.
 */
class BranchesController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

 
    public function actionLists($id)
    {
        
        $countBranches = Branches::find()
                ->where(['companies_company_id' => $id])
                ->count();

        $branches = Branches::find()
                ->where(['companies_company_id' => $id])
                ->orderBy('branch_id DESC')
                ->all();

        if($countBranches>0){
            foreach($branches as $branch){
                echo "<option value='".$branch->branch_id."'>".$branch->branch_name."</option>";
            }
        }
        else{
            echo "<option>-</option>";
        }
        
        //echo "<option>-</option>";

    }
 
 
.....
.....
.....

}





Yii 2.0: Creating a Dependent Dropdown From Scratch in Yii2


Related Links

YII2 CRUD Operations code with Gii
DropDownList

Yii2 Pjax Tutorial

Yii Tabular Input

Updating Gridview Using Pajax
Multiple submit button in Yii2 view forms



3 comments:

  1. Any idea how to retain the data from dependent child dropdown ?

    ReplyDelete
    Replies
    1. I mean retrieving the data when update function view is called.

      Delete