Best way to save all the items in the target List box

Dear All,

I have a case where in a view, user moves the desired items from source drop down list to target drop down list

Example :- Cars

Source drop down list contains 100 car models and user selects his best models and moves into target drop down list , similar to

http://www.meadmiracle.com/dlb/DLBPlugin.aspx

Code example


<?php echo CHtml::dropDownList("sourceCars", '',CHtml::listData(MasterCars::model()->findAll(),'cars_code','car_name'),array('size'=>20) );?>


<?php echo CHtml::dropDownList("targetCars", '', array(),array('size'=>20) );?>

Questions

  1. How can I access all the targetCars list items in my controller

  2. What is the best way to save them to database using model ( table has simply two columns , user_id and car_id)

Thanks a lot for your help

( I read the documentation and I know how to get the selected value in the drop down list in controller , but here the case is moving from one list to another list and selecting all of them in the target list)

Regards

Yii Fan ( Recently started learning it )

I tend to only use one multiselect dropdown that I dress up with jQuery (for example: http://quasipartikel.at/multiselect/ ). The selected elements are then passed down to the controller:


echo CHtml::dropDownList("sourceCars",

                          '',

                          CHtml::listData(MasterCars::model()->findAll(),'cars_code','car_name'),

                          array('multiple'=>'multiple',// added 'multiple'

                                'size'=>20

                                //'class'=> 'multiselect' if you use the above jquery widget

                                )

                          );

If you want to avoid doing that for whatever reason, adding ‘multiple’ to targetCars might do the trick. You will need to make sure your javascript marks whichever items it switches over as selected in the new box.


echo CHtml::dropDownList("targetCars", '', array(),array('multiple'=>'multiple','size'=>20) );

Given what you said about your table I think you’ll need to loop through the POST data to insert new rows for each item.

Just in case, you will find some more information on how to process the data in your controller (model in fact) if you want to convert the POST array you get to a string automatically before save: http://www.yiiframework.com/forum/index.php?/topic/5160-how-to-implement-select-multiple-listbox/

Thank You pommeverte for your response . I used Jquery and collected all the input and called controller from there . Things are working fine . Thanks for your advise

Regards

Kiran