PHP code example of zrk4939 / yii2-depdrop

1. Go to this page and download the library: Download zrk4939/yii2-depdrop library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zrk4939 / yii2-depdrop example snippets



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')]),
])


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