How to load selected list items in multiple-select-listbox in update view in yii?
Asked Answered
G

1

8

I have a multiple select-list-box for Staff in Create-Service-Form, used to select multiple staff when creating a new service. for this i can assign multiple staff on a single service.


I saved staff_id field as:
$model->staff_id = serialize($model->staff_id);


Here the update-view code for multiple-select-list-box:

<div class="row">
    <?php echo $form->labelEx($model,'staff_id'); ?>
    <?php
          $data = array('1' => 'Sam', '2' => 'john', '3' => 'addy');
          $htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'multiple');
          echo $form->ListBox($model,'staff_id', $data, $htmlOptions); 
    ?>
    <?php echo $form->error($model,'staff_id'); ?>
</div>

Problem is, when i load form for updating a service. how do i select those staff, which are previously saved in database?

I tried this dropDownList-attributes, but it not working.
$select | string | the selected value

if someone has solution, then suggest me. Thanks All Mates...

Gingery answered 2/7, 2012 at 12:46 Comment(0)
J
12

Here's a quick code I wrote for you, its an example that will help you understand how it works.

<div class="row">
  <?php echo $form->labelEx($model,'staff_id'); ?>
  <?php 
    $data = array('101' => 'Faraz Khan', '102' => 'Depesh Saini', '103' => 'Nalin Gehlot', '104' => 'Hari Maliya');
    $selected   = array(
      '102' => array('selected' => 'selected'),
      '103' => array('selected' => 'selected'),
    );
    $htmlOptions = array('size' => '5', 'prompt'=>'Use CTRL to Select Multiple Staff', 'multiple' => 'true', 'options' => $selected);
    echo $form->listBox($model,'staff_id', $data, $htmlOptions);
  ?>
  <?php echo $form->error($model,'staff_id'); ?>
 </div>

Have Fun Ya !!!

Jaella answered 3/7, 2012 at 6:33 Comment(2)
Any simple method to do it with values from database. Mostly master-detail.Frequently
Anyone have the Yii2.x syntax for this handy?Fogged

© 2022 - 2024 — McMap. All rights reserved.