S
S
slip312014-05-20 15:09:41
Yii
slip31, 2014-05-20 15:09:41

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');
    }
and
public 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');
       }

In the view, I display the usual countries:
<?= $form->field($model, 'country')->dropDownList($model->CountryList, ['id'=>'cat-id']) ?>

In the next field, you need to already use DepDrop:
<?= $form->field($model, 'city')->widget(DepDrop::classname(), [
    'options'=>['id'=>'subcat-id'],
    'pluginOptions'=>[
        'depends'=>['cat-id'],
        'placeholder'=>'Select...',
        'url'=>Url::to(['/site/subcat'])
    ]
]);

There is also a controller:
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'=>'']);
}

I don't understand what to put where. 'cat-id', of course, is a dependency, in fact country_id and then a stupor. Help explain please

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Zelenin, 2014-05-20
@slip31

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');
       }

apparently so

C
Carlos Rodriguez, 2015-01-14
@asf

slip31 :
www.yiiframework.com/wiki/723/creating-a-dependent...

V
Vadim Shurman, 2015-05-31
@VaShu

video tutorial on this topic
yii-02.blogspot.com/2015/01/yii2-lesson-20-depende...

S
slip31, 2014-05-30
@slip31

So, how to do dropDown without third party widget in Yii2?
There are examples for yii1, but not for yii2. If you just have two tables 'id','name' and 'id','parent_id','id'.
For yii1 there is This example , but it cannot be rewritten directly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question