J
J
Julia2021-06-24 15:56:01
Yii
Julia, 2021-06-24 15:56:01

How to run DepDrop kartik yii2 scenario 3?

Hello! Please help me deal with depdrop scenario 3 from kartik.

There are 2 tables: Executor (id, executor) and Directory (id, directory, id_executor)

I do according to the kartik documentation, as a result, the drop-down lists are independent of each other.

// THE VIEW
// Top most parent
<?= $form->field($model, 'id_executor')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(Executor::find()->groupBy('executor')->asArray()->all(), 'id', 'executor'),  'options' => ['placeholder' => 'Выберите исполнителя ...']]);?>

// Child 
    <?= $form->field($model, 'directory')->widget(DepDrop::classname(), [
        'data' => ArrayHelper::map(Directory::find()->groupBy('directory')->asArray()->all(),'id', 'directory'),
        'options' => ['placeholder' => 'Выберите должника ...'],
        'type' => DepDrop::TYPE_SELECT2,
        'select2Options' => ['pluginOptions' => ['allowClear' => true]],
        'pluginOptions' => [
            'depends' => ['select2-directory-id_executor-container'],
            'url' => Url::to(['/directory/directory']),
            'loadingText' => 'Загрузка ...',
        ]
    ]);?>

// CONTROLLER
public function actionChildAccount() {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $id = end($_POST['depdrop_parents']);
            $list = Directory::find()->andWhere(['id_executor' => $id])->asArray()->all();
            $selected  = null;
            if ($id != null && count($list) > 0) {
                $selected = '';
                foreach ($list as $i => $model) {
                    $out[] = ['id' => $model['id'], 'executor' => $model['executor']];
                    if ($i == 0) {
                        $selected = $model['id'];
                    }
                }
                // Shows how you can preselect a value
                return ['output' => $out, 'selected' => $selected];
            }
        }
        return ['output' => '', 'selected' => ''];
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question