Search code examples
yiimultiple-selectmulti-selectyii-inheritance

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


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...


Solution

  • 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 !!!