Answer the question
In order to leave comments, you need to log in
How to make dependent dropdown Yii2 (Yii2 DepDrown)?
Could you help with the script?
Making a linked list of countries, cities using
DepDrop .
Everything seems to be chewed up, but I can’t cut it in any way. You need to make a dependency.
Methods are registered in the model:
public function getCountryList() { // could be a static func as well
$models = \common\models\Country::find()->asArray()->all();
return ArrayHelper::map($models, 'id', 'name_ru');
}
andpublic function getCityList() { // could be a static func as well
$models = \common\models\City::find()->asArray()->all();
return ArrayHelper::map($models, 'id', 'name_ru','country_id');
}
<?= $form->field($model, 'country')->dropDownList($model->CountryList, ['id'=>'cat-id']) ?>
<?= $form->field($model, 'city')->widget(DepDrop::classname(), [
'options'=>['id'=>'subcat-id'],
'pluginOptions'=>[
'depends'=>['cat-id'],
'placeholder'=>'Select...',
'url'=>Url::to(['/site/subcat'])
]
]);
public function actionCity() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = self::getCityList($cat_id);
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
// ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
// ]
echo Json::encode(['output'=>$out, 'selected'=>'']);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
Answer the question
In order to leave comments, you need to log in
public function getCityList($cat_id) { // could be a static func as well
$models = \common\models\City::find()->where(['country_id' => $cat_id])->asArray()->all();
return ArrayHelper::map($models, 'id', 'name_ru','country_id');
}
video tutorial on this topic
yii-02.blogspot.com/2015/01/yii2-lesson-20-depende...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question