Dynamic listbox trouble

Hi All,

i have encountered some issues with using listBox dynamically. what really i wanted to do is, to pre select some options from listbox and load the page. for example,

<select name="drop1" id="Select1" size="4" multiple="multiple">

&lt;option value=&quot;1&quot;&gt;item 1&lt;/option&gt;





&lt;option selected=&quot;selected&quot; value=&quot;2&quot; &gt;item 2&lt;/option&gt;





&lt;option selected=&quot;selected&quot; value=&quot;3&quot; &gt;item 3&lt;/option&gt;





&lt;option value=&quot;4&quot;&gt;item 4&lt;/option&gt;





&lt;option value=&quot;0&quot;&gt;All&lt;/option&gt;

</select>

from this code i wanted to pre select item 2 and item 3 on page load. where all the items are coming from a table.

this is my code block.

<?php echo $form->listBox($model, ‘roles’, CHtml::listData(AuthItem::model()->findAllBySql(‘select name,description from auth_items where type = 2’), ‘name’, ‘description’), array(‘multiple’=>‘Select’,

                          'options'=&gt;array(


                         


                          [b]'rAccountant'=&gt;array('selected'=&gt;'selected'),


                          'rAdmin'=&gt;array('selected'=&gt;'selected')[/b]


                          )));

?>

i want those two options to be selected when coming from database.

i am having some difficulties on writing the code as follows,

[b]<?php echo $form->listBox($model, ‘roles’, CHtml::listData(AuthItem::model()->findAllBySql(‘select name,description from auth_items where type = 2’), ‘name’, ‘description’), array(‘multiple’=>‘Select’,

                          'options'=&gt;array(


                        [size=&quot;4&quot;] foreach (&#036;ietm as &#036;temp){


                             '&#036;temp'=&gt;array('selected'=&gt;'selected')


                             echo &quot;&quot;;


                             


                         }[/size]


                      


                          )));

?>[/b] where $temp is an array and will have one or two or three items. so according the items in $temp i wanted them to be selected by default.

please help me with this issue. i am newbie to YII framework and struggling with this section badly.

many thanks in advance.

waiting for a reply. :(

Hi, if it’s possible to use jQuery you could do it like this:


$(document).ready(function(){	

	$("#modelname_roles option[value='1']").attr("selected", "selected");

	$("#modelname_roles option[value='3']").attr("selected", "selected");

}

[size="4"][b]

tnx alot. but i wanted it from PHP… :(

is thr any other way to do this? am i the only person having this type of issue ? i totally searched in Yii forum and cdnt find out a way to do this task.

pls help me out. tnx [/b][/size]

Hi, you can probably solve it in regular php. See this link:

tnx. but according to yii’s cform structure i dont think this will help me out. because in yii its defined inside an array like below,

‘options’=>array(

‘rAccountant’=>array(‘selected’=>‘selected’),

‘rAdmin’=>array(‘selected’=>‘selected’)

i want it to be automated like in this… from a list of item i want only a certain items to be marked as selected in listbox, pls let me know a way… tnx

[size=“5”]didnt get a proper answer yet. can any one pls throw me a bone.[/size] :(

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


    $data   = array(

      '1' => 'One',

      '2' => 'Two',

      '3' => 'Three',

    );

    $selected   = array(

      '1' => array('selected' => 'selected'),

      '3' => array('selected' => 'selected'),

    );

    echo CHtml::activeListBox(new Department, 'departmentID', $data, array('multiple' => 'multiple', 'options' => $selected));

hey thanks alot rookie84… both way it worked. :)

i tried with jquery too, which xajaccio stated.

both works perfectly. many thanks again :)

Its really help full Thank you rookie84!