yii2-dynamicform
yii2-dynamicform copied to clipboard
set default value of dropDownList which is a part of dynamic Form
How to set default Value of dropDownList field I have tried
- set the attribute of Model[0] in controller
- $addressModel->city_id = 4 before the field
$form->field($addressModel, "[{$index}]city_id")->dropDownList(
ArrayHelper::map(City::find()->all(), 'id', 'name'))
- also
$form->field($addressModel, "[{$index}]city_id")->dropDownList(
ArrayHelper::map(City::find()->all(), 'id', 'name'), ['options' => [
4 => ['Selected' => true],
],
])
but nothing works
Solution
$(function () {
$(".dynamicform_wrapper").on("afterInsert", function(e, item) {
$(item).find("select[id$='-city_id']").val(4);
});
});