0

I am using HTML select tag within a pageblocktable in my VF page to display a wrapperclass list. How can i pass the selected values into the wrapper class in the controller?

AND

How can preselect the options of the select with values from my wrapper class?

I know it would be easier for me to use the Apex:selectList but i wanted to use Select2 jquery plugin to get a nice look and feel.

 <apex:pageblockTable value="{!selectedProducts}" var="s" id="QLIPBT">
 <apex:column headerValue="Targeting" style="width:120px;">
                       <!-- <apex:inputField value="{!s.quotelineitemRec.Targetting__c}" style="width:100px" required="true" /> -->
                       <apex:inputHidden value="{!s.selectedTargetGroup}" />
                        <select multiple="multiple" name="Target" style ="width:120px;"  onchange ="document.getElementById('{!$Component.hiddenTarget}').value = this.value; checkSelected();">
                            <apex:repeat value="{!s.TargetingOptions}" var="stage">
                               <option value="{!stage.value}">{!stage.label}</option> 
                            </apex:repeat>
                        </select>
                    </apex:column>

</apex:pageblockTable>

Controller wrapperclass

 public class selectedProductWrapper
     {

        public List<SelectOption> TargetingOptions {get; private set;}
        public quoteLineItem quotelineitemRec {get; private set;}
        public Id Pricebook2EntryId  {get; private set;}
        public String TargetGroup  {get; private set;}
        public String selectedTargetGroup  {get; set;}
        public string selTargeting  {get; set;}

        public selectedProductWrapper( quoteLineItem quotelineitemRec_v,String TargetGroup_v)
        {
            this.quotelineitemRec = quotelineitemRec_v;
            this.Pricebook2EntryId = quotelineitemRec.PriceBookEntryId;
            this.TargetGroup = TargetGroup_v;
            TargetingOptions = new  List<SelectOption>();
            for(Group_Members__c gm : [SELECT name FROM Group_Members__c WHERE Product_Target_Group__c =:TargetGroup])
            {
                this.TargetingOptions.add(new Selectoption(gm.name,gm.name));

            }
            this.selectedTargetGroup = quotelineitemRec.Targetting__c;
            system.debug('selected Options ' + TargetingOptions);
        }
     }
3
  • 1
    One of the ideas may be create inputHidden for some string variable in controller, and actionfunction to rerender that hidden input. via JS you put value to that input hidden, call actionfunction that update variable in controller Commented Mar 4, 2016 at 8:43
  • I think that is one way which i was moving towards, but i am totally stumped on how i can preselect options of html select from value in controller. Commented Mar 4, 2016 at 8:47
  • probably the same way - one more hidden variable, which you can fill in constructor, and grab on ready from VF page via JS Commented Mar 4, 2016 at 8:54

1 Answer 1

1

You can use:

<option value="{!stage.value}"
        {! IF(stage.value == selTargeting, 'selected', '') }
        >{!stage.label}</option>

i.e. make use of the HTML option selected attribute assuming you set selTargeting to the value you want selected in the wrapper class.

6
  • Thanks Keith. I will try that out. I do have another question, i seem to be getting the first value selected in the hidden field even if i have selected multiple values in the Select. Any thought why ? Commented Mar 4, 2016 at 11:10
  • @Prady I don't see any code that tries to synchronize what is selected with the hidden field. By the way, are you sure you can't get Select2 to work with an apex:selectList? If you did then the initial state and saving of the selection(s) would just work. Commented Mar 4, 2016 at 11:25
  • My bad, i included that code in now. Somehow the first selected value is only pushed into the hidden field, anything selected after that is not added Commented Mar 4, 2016 at 11:47
  • @Prady See e.g. stackoverflow.com/questions/11821261/…. Commented Mar 4, 2016 at 11:51
  • Regarding your earlier comment on binding selectList with Select2, i couldnt add the attribute multiple to apex:selectList. Thanks for the link. Can i bind an array into an apex:inputfield ? Commented Mar 4, 2016 at 11:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.