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?
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.
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:
The code for the view:
The code for the Controller:
Here I needed to change the
'selected' => ''
to the'selected' => reset($out)['id']]);
or'selected' => '1'
As error I got in my console:
I tracked the error in the javascript to line 191:
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