kartik-v / yii2-widget-depdrop

Widget that enables setting up dependent dropdowns with nested dependencies (sub repo split from yii2-widgets).
Other
81 stars 39 forks source link

DepDrop not working with no default value #52

Closed eothein closed 5 years ago

eothein commented 7 years ago

Dear Kartik

I have been using the widget for some time now, but after a recent update, it stopped working.

It seems that there are two problems:

  1. It is not allowed to not provide a default value anymore in the AJAX response?
  2. When a related value does yet exist, there are rendering problems of the deprop.

The code for the view:

reservatie_contactpersoonid' => ['type' => Form::INPUT_WIDGET, 'widgetClass' => DepDrop::classname(),
                                        'options' => [
                                            'id' => 'contactpersoon',
                                            'pluginOptions' => [
                                                'depends' => ['reservatie-reservatie_klantid'],
                                                'placeholder' => 'Selecteer',
                                                'initialize' => false,
                                                'url' => Url::to(['/klant/contactpersoonforklant'])
                                            ]
                                        ],
                                    ],

The code for the Controller:

public function actionContactpersoonforklant() {
        $out = [];
        \Yii::$app->response->format = 'json';
        if (isset($_POST['depdrop_parents'])) {
            $parents = $_POST['depdrop_parents'];
            if ($parents != null) {
                $klant_id = $parents[0];
                $klant = Klant::findOne(['klant_id' => $klant_id]);
                $out = $klant->getKlantContactpersoonContactpersoons()->select(['contactpersoon_id AS id', 'contactpersoon_naam as name'])->asArray()->all();
                echo Json::encode(['output' => $out, 'selected' => reset($out)['id']]);
                return;
            }
        }
        echo Json::encode(['output' => '', 'selected' => '1']);
        return;
}

Here I needed to change the 'selected' => '' to the'selected' => reset($out)['id']]);or 'selected' => '1'

As error I got in my console:

dependent-dropdown.js:191 Uncaught TypeError: Cannot read property 'toString' of null at DepDrop.createOption (dependent-dropdown.js:191) at DepDrop.renderSelect (dependent-dropdown.js:203) at Object.success (dependent-dropdown.js:151) at fire (jquery.js:3187) at Object.fireWith [as resolveWith] (jquery.js:3317) at done (jquery.js:8757) at XMLHttpRequest. (jquery.js:9123)

I tracked the error in the javascript to line 191:

 } else {
                selIds = [sel.toString()];
}

If the default value is not provided in the JSON response, the sel value is null and the toString throws an exception.

Is this correct and if so, how do I work with unselected default data or related data which is not yet in the database?

Kind regards

Jens Buysse

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.