Nana Partykar
Nana Partykar

Reputation: 10548

Show selected options in multiselect dropdownlist - Yii2

I am using multiselect dropdown for selecting more than 1 option. Till this Ok. But I'm stuck, when I need to show those selected options in view page.

I tried.

<?
public function getSelectedTodosCaseId($todos_id) {
    $case_ids = Todos::find()->select(['case_id'])->where(['todos_id'=> $todos_id])->all();
    $selected = [];
    if($case_ids){
        foreach($case_ids as $case_id){
            $selected[] = $case_id->case_id ;
        }
    }
    return $selected;
}
?>

I'm using this function here.

<?php

$selectedCase = $this->getSelectedTodosCaseId($id);

for($i = 0; $i < sizeof($selectedCase); $i++){
  $model->t_case_id = $selectedCase[$i];
}
?>

<?= $form->field($model, 't_case_id[]',
                          ['options' =>['class' => ''],'inputOptions'=>['multiple'=>'multiple','size'=>'4']])
                          ->dropDownList($current_user_cases);?>

Any help/hint would be appreciable.

Upvotes: 2

Views: 1228

Answers (1)

Nana Partykar
Nana Partykar

Reputation: 10548

In short just remove array(square brackets) from field t_case_id[]. simply give t_case_id, no need it will be automatically converted to array. ~ @KandarpPatel

$model->t_case_id = $selectedCase;
<?= $form->field($model, 't_case_id',
         ['options' =>['class' => ''],'inputOptions'=>['multiple'=>'multiple','size'=>'3']])
         ->dropDownList($current_user_cases)->label("Related to");?>

I didn't provided dropdown name as array type. But, it was automatically converted. And, It worked like a charm.

Upvotes: 1

Related Questions