Please how do I tie two dependent drop down to one
For Example, I have
-
State (Main one)
-
City (dependent)
-
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