yii2-dynamicform icon indicating copy to clipboard operation
yii2-dynamicform copied to clipboard

set default value of dropDownList which is a part of dynamic Form

Open jainabhishek14 opened this issue 7 years ago • 1 comments

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

jainabhishek14 avatar Jun 15 '17 18:06 jainabhishek14

Solution

$(function () {
    $(".dynamicform_wrapper").on("afterInsert", function(e, item) {
        $(item).find("select[id$='-city_id']").val(4);
    });
});

sokollondon avatar Nov 22 '23 04:11 sokollondon