how to initialize the dependent dropdown fields with preselected data from database

hello Yii developers,

I’m here to ask you a question on a problem statement i.e.

I’ve developed a dependent dropdownlist module in YII 2.0 framework, with three levels as Country, State, City.

this is working perfectly, but now i want to do one more thing with this is that i want to initialize the dependent dropdown fields with preselected data, fetched from database.

can anybody help me out with this, or any references that would be helpful for me to work with this.

I have the same requirement.

I solved it by below code it mayhelp u.


	echo  $form->field( $model, 'countryId',['showLabels'=>false])->dropDownList

		(ArrayHelper::map( Country::find()->all(), 'countryId', 'countryName'),   

		['id'=>'dropdownCountry', 'class'=>'ddClass']) ;

		$item =  ArrayHelper::map( State::find()->where(['countryId' => '1'])->all(), 'stateId', 'stateName');//preselected

	echo $form->field($model, 'stateId')->widget(DepDrop::classname(), [ 

		'data' => [$item],

		'type' => DepDrop::TYPE_SELECT2,

		'select2Options'=>['pluginOptions'=>['allowClear'=>true]],

		'pluginOptions'=>[

		'depends'=>['dropdownCountry'],

		'url' => Url::to(['subcat1']),

		'loadingText' => 'Loading child level 1 ...', ] ]); 

rups g