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

我只是一个虾纸丫 提交于 2019-12-07 01:00:37

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/11293990/how-to-load-selected-list-items-in-multiple-select-listbox-in-update-view-in-yii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!