Dependent dropdownlist

Please how do I tie two dependent drop down to one

For Example, I have

  1. State (Main one)

  2. City (dependent)

  3. LGA (Dependent)

Once I click on State, it should populate both City and State, at the same time.

Contoller: Dependent




	public function actionHospital_city($id){


		$rows = \app\models\City::find()->where(['city_state_id' => $id, 'is_status' => 0])->all();

	 

		echo "<option value=''>".Yii::t('app', '--- Select City ---')."</option>";

	 

		if(count($rows)>0){

		    foreach($rows as $row){

		        echo "<option value='$row->city_id'>$row->city_name</option>";

		    }

		}

		else{

		    echo "";

		}

 

    	}




model




    public function attributeLabels()

    {

        return [

            'hospital_id' => 'Hospital ID',

            'hospital_name' => 'Hospital Name',

            'description' => 'Description',

            'address' => 'Address',

            'city_id' => 'City',

             'lga_id' => 'Lga',

            'state_id' => 'State',


    }



View




            <div class="col-sm-4">

        <?php echo $form->field($model,'state_id',['inputOptions'=>[ 'class'=>'form-control'] ])->dropDownList(ArrayHelper::map(\app\models\State::find()->all(),'state_id','state_name'),

            [

                    'prompt'=> Yii::t('app', '--- Select State ---'),

                    'onchange'=>'

                        $.get( "'.Url::toRoute('dependent/hospital_city').'", { id: $(this).val() } )

                            .done(function( data ) {

                                $( "#'.Html::getInputId($model, 'city_id').'" ).html( data );

                            }

                        );'    

                ]       

                ); 

             ?>

          

            </div>

            <div class="col-sm-4">


        <?php if(isset($model->city_id)) { ?>


        <?= $form->field($model, 'city_id',['inputOptions'=>[ 'class'=>'form-control'] ])->dropDownList(ArrayHelper::map(\app\models\Lga::find()->all(),'city_id','city_name'),['prompt'=>Yii::t('app', '--- Select City ---')]);  ?>

        <?php } else {   ?>

        <?= $form->field($model, 'city_id',['inputOptions'=>[ 'class'=>'form-control'] ])->dropDownList(['prompt'=>Yii::t('app', '--- Select City ---')]);  ?>

        <?php } ?>

            </div>        

        </div>  



This will populate only city. How do I make it populate oth city and LGA

if you mean like you need to populate two child dropdowns on change of parent dropdown.

then use these steps

  1. Make your action like "ActionGetCityLga()" which returns an array of two HTML.

[indent]e.g




$result = [];

++++++

Do your logic for get city's HTML like what you did in actionHospital_city()

array_push($result,$city_options);


Do your logic for get LGA's HTML like what you did in actionHospital_city()

array_push($result,$lga_options);


echo Json::encode($result);


++++++ 



[/indent]

  1. On you view file where you wrote "jQuery Ajax call" get this JSON array of both HTML

and now populate 0th index as city and 1st index as LGA.