zrk4939/yii2-depdrop

The dependent dropdown widget for the Yii framework 2

Installs: 189

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

1.0.3 2019-02-28 09:36 UTC

This package is auto-updated.

Last update: 2024-03-28 21:31:33 UTC


README

A Dependent DropDown widget for the Yii2 framework

Installation

Add

"zrk4939/yii2-depdrop": "@dev"

to the require section of your composer.json file and execute composer update.

Usage

Widget

<?php
echo $form->field($model, 'attribute')->widget(\zrk4939\widgets\depdrop\DepDrop::className(), [
    'placeholder' => 'Выбрать из списка...',
    'depends' => ['id-dependent-field'],
    'url' => \yii\helpers\Url::to(['/ajax/get', 'selected' => $model->getAttribute('attribute')]),
])
?>

Controller

<?php
public function actionGet()
{
    Yii::$app->response->format = Response::FORMAT_JSON;
    
    $result = [
        'output' => [],
        'selected' => Yii::$app->request->get('selected')
    ];
    
    if (Yii::$app->request->isPost) {
        $post = Yii::$app->request->post('depdrop_all_params');
        $query = SomeModel::find()
                ->andWhere(['attribute' => $post['attribute']]);
        
        foreach ($query->all() as $model) {
            $result['output'][] = [
                'id' => $model->getAttribute('id'),
                'name' => $model->getAttribute('name'),
            ];
        }
    }
    
    return $result;
}
?>