2017-02-10 8 views
1

私はYii2に取り組んでいます。私はこのようなカスタム配列を使用して複数のselectドロップダウンを作成しています。コントローラファイルでYii2アプリで複数選択ドロップダウンで選択した値を表示するにはどうすればいいですか?

:このような

return $this->render('mngr_userupdate', [ 
         'model' => $model, 
         'all_groups_array'=>$all_groups_array, 
         'case'=>$case, 
         'email_error' => 'false', 
         'applied_email' => '' , 
         ]); 

ので、作成されたアレイ:

Array 
(
    [11] => Mngr1 group 
    [14] => Mngr 11 Group 
) 

では、ファイルのVIE:

$all_groups = Groups::find()->where(['=','group_created_by',$id])->orwhere(new Expression('FIND_IN_SET(:id_to_find, group_managers)'))->addParams([':id_to_find' => $id])->all(); // fetch all values 

$selected_groups = Groups::find()->where(['=','group_users',$updateId])->orwhere(new Expression('FIND_IN_SET(:id_to_find, group_users)'))->addParams([':id_to_find' => $updateId])->all(); // getting selected values 

$all_groups_array = []; 

foreach ($all_groups as $group) { 
    $all_groups_array[$group->id] = ucfirst($group->group_name); 
} 

では、ビューにデータをレンダリング

<?= $form->field($model, 'group_user[]')->dropDownList($all_groups_array,['multiple' => 'multiple']) ?> 

データ挿入用のフォームを作成する際に問題なく動作しています。どのように私は更新フォームで選択した値を表示することができますを使用して配列を作成する。

編集:

私はちょうど私が

<?= $form->field($model, 'group_user[]')->dropDownList($all_groups_array,['multiple' => 'multiple', 'options'=>['14'=>["Selected"=>true],'11' => ["Selected"=>true]]]); ?> 

のようにそれを使用する場合、選択として、それが表示値を開始することを発見しました。私はループを使用して、この配列を作成するにはどうすればよい

foreach ($selected_groups as $key => $value) { 
      $sel_groups_array[$value] = '' // what should be there or else 
      } 

:つまり私は、私は次のようにのようなループを使用しています。このため

[ 
'14'=>["Selected"=>true], 
'11' => ["Selected"=>true] 
] 

のような配列を作成する必要がありますか?

答えて

1

私は誰もが問題のような種類を持っている場合は次のように、彼はのようなループを使用することができます場合は、私の質問の解決策を作成しました:

foreach ($selected_groups as $group) { 

    $sel_groups_array[$group->id] = array("selected"=>true); 
} 

とビューには、あなたがのために、配列を使用することができますファイル次のように選択された複数の値を表示:構造は、更新フォーム上の複数の選択された値を表示する

<?= $form->field($model, 'group_user[]')->dropDownList($all_groups_array,['multiple' => 'multiple','options' => $sel_groups_array]); ?> 

ので、次のようにのようにすべきである:

$form->field($model, 'group_user[]')->dropDownList($all_groups_array,['multiple' => 'multiple', 'options'=>['14'=>["Selected"=>true],'11' => ["Selected"=>true]]]); 
// here 14 and 11 I am using as example 
関連する問題