6
votes

I am using the Kartik Depdrop widget.

Everything is working fine except in the situation where I have not selected a value on creation of new record, on update the dependent field should show Please select whereas it is showing the first value in the drop-down and it is getting saved on update, whereas I want the value of 'please select' i.e. null to be saved even on update depending on the situation.

Example- I select a room-category - dependant value is packages, which can be null as well. So I didn't select any value in the dependent field packages. But when I come to update the form the first value in the drop-down is showing by default, whereas I want the please select as the default.

How can correct this?

$form->field($model, 'package')->widget(DepDrop::classname(), [                            
   'data'=>ArrayHelper::map(\app\models\Package::find()->all(), 'id', 'package_name' ),
   'pluginOptions'=>[
   'depends'=>['room_category'], 
   'placeholder'=>'Select...',
   'url'=>  \yii\helpers\Url::to(['patient-detail/subcat']),               
    ]
    ])

Note:If I am selecting a value in the dependant dropdown on creation, then the value on update is showing correctly.

2
I think that your problem may be that you've set the data for your dependent drop-down. As I understand the widget, you only set the data for the first drop-down, then it uses ajax calls to get the data for the subsequent drop-downs. - Joe Miller
Hi Joe Miller, as I stated that the dependent dropdown is working fine if I select the data at the time of creation. only problem is if I have not selected any data from the dependent dropdown at the time of creation, then in the update screen all the data is shown and the first field is occupied the value and getting saved on update, whereas the acceptable behavior should be that it should show select in the update screen, if no data is selected at the time of creation. - Pawan

2 Answers

4
votes

It works well for me.

View file

<?php echo $form->field($model, 'area_parent_id')->dropDownList(AreaCode::getProvinceOption(), ['prompt' => 'select...', 'id' => 'parent_id']); ?>

<?php echo Html::hiddenInput('selected_id', $model->isNewRecord ? '' : $model->area_id, ['id'=>'selected_id']); ?>

<?php echo $form->field($model, 'area_id')->widget(\kartik\depdrop\DepDrop::classname(), [
    'options' => ['id' => 'area_id', 'class' => '', 'style' => ''],
    'pluginOptions' => [
        'depends' => ['parent_id'],
        'placeholder' => 'select...',
        'initialize' => $model->isNewRecord ? false : true,
        'url' => Url::to(['/area-code/subcat']),
        'params'=> ['selected_id'], 
    ],

]); ?>

Controller file

public function actionSubcat()
{
    $out = [];
    if (isset($_POST['depdrop_all_params'])) {
        $parent_id = $_POST['depdrop_all_params']['parent_id'];
        $selected_id = $_POST['depdrop_all_params']['selected_id']; 
        $out = AreaCode::find()->select(['id', 'name'])->where(['parent_id' => $parent_id])->asArray()->all();
        return \yii\helpers\Json::encode(['output' => $out, 'selected' => $selected_id]);
    }

    return \yii\helpers\Json::encode(['output' => '', 'selected' => '']);
}
1
votes

Okay this is the solution you want to do:

// THE VIEW
use kartik\widgets\DepDrop;

// Top most parent
echo $form->field($account, 'lev0')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Account::find()->where('parent IS NULL')->asArray()->all(), 'id', 'name')
]);

//ADDITIONAL PARAM ID YOU MAY USE TO SELECT A DEFAULT VALUE OF YOUR MODEL IN YOUR DEPDROP WHEN YOU WANT TO UPDATE:
echo Html::hiddenInput('model_id1', $model->id, ['id'=>'model_id1' ]);

// Child level 1
echo $form->field($account, 'lev1')->widget(DepDrop::classname(), [
'data'=> [6 =>'Bank'],
'options' => ['placeholder' => 'Select ...'],
'type' => DepDrop::TYPE_SELECT2,
'select2Options'=>['pluginOptions'=>['allowClear'=>true]],
'pluginOptions'=>[
    'depends'=>['account-lev0'],
    'url' => Url::to(['/account/child-account']),
    'loadingText' => 'Loading child level 1 ...',
    'params'=>['model_id1'] ///SPECIFYING THE PARAM
]
]);

// CONTROLLER
public function actionChildAccount() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
    $id = end($_POST['depdrop_parents']);
    $list = Account::find()->andWhere(['parent'=>$id])->asArray()->all();
    $selected  = null;
    if ($id != null && count($list) > 0) {
        //EXACTLY THIS IS THE PART YOU NEED TO IMPLEMENT:
        $selected = '';
        if (!empty($_POST['depdrop_params'])) {
            $params = $_POST['depdrop_params'];
            $id1 = $params[0]; // get the value of model_id1

            foreach ($list as $i => $account) {
                $out[] = ['id' => $account['id'], 'name' => $account['name']];
                if ($i == 0){
                    $aux = $account['id'];
                }

                ($account['id'] == $id1) ? $selected = $id1 : $selected = $aux;
            }
        }
        // Shows how you can preselect a value
        echo Json::encode(['output' => $out, 'selected'=>$selected]);
        return;
    }
}
echo Json::encode(['output' => '', 'selected'=>'']);
}

Ensure this process to be executed when the page is completely loaded.