W
W
walder2018-10-29 12:53:17
Yii
walder, 2018-10-29 12:53:17

How to save value in depdrop after page reload?

Hello.
There is a filter on the page, two selects dependent on each other (Select2 and DepDrop). If you select from the filter, everything works well, the table is filtered. And if, for example, we go to the details of a record and go back or just refresh the page, then the second select (DepDrop) is reset, it just becomes disabled and shows the placeholder, but the records are displayed correctly, that is, the ones that were filtered. How to save the selection of the second select? So that after refreshing the page, the selected value remains?
view

<div class="col-lg-6 col-md-6">
    <?php echo $form->field($model, 'Cat')
    ->label(false)
    ->widget(Select2::class, [
        'data'=> Orders::getCat(),
        'value' => '',
        'options' => [
            'id'=>'cat_id',
            'placeholder' => 'Cat...',
        ],
        'pluginOptions' => [
            'allowClear' => true,
        ],
    ]) ?>
</div>

<div class="col-lg-6 col-md-6">
    <?php
    echo $form->field($model, 'sub')
        ->widget(DepDrop::class, [
            'type' => DepDrop::TYPE_SELECT2,
            'select2Options' => [
                'pluginOptions'=>['allowClear'=>true,]
            ],
            'options'=>[
                'id'=>'sub_id'
            ],
            'pluginOptions'=>[
                'depends'=>['typ-id'],
                'placeholder'=>'Sub...',
                'url'=>Url::to(['orders/subcat']),
            ],
        ])->label(false);
    ?>
</div>

controller
public function actionSubcat() {
    $out = [];

    if (isset($_POST['depdrop_parents'])) {
        $parents = $_POST['depdrop_parents'];

        if ($parents !== null) {
            $cat_id = $parents[0];
            $listCat = Orders::findCatId($cat_id);
            $out[] = ['id' => $value['id'], 'name' => $value['name']];

            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

1 answer(s)
A
Andrey Sedyshev, 2018-10-29
@walder

Try to specify for the second select

'pluginOptions' => [
                'depends'=>['typ-id'],
                'placeholder'=>'Sub...',
                'initialize' => true,
                'initDepends'=>['Cat'],
                'url'=>Url::to(['orders/subcat']),
            ],

Example link: demos.krajee.com/widget-details/depdrop#advanced-usage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question