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);
        }
     }
  • 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 – kurunve Mar 4 '16 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. – Prady Mar 4 '16 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 – kurunve Mar 4 '16 at 8:54
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.

  • 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 ? – Prady Mar 4 '16 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. – Keith C Mar 4 '16 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 – Prady Mar 4 '16 at 11:47
  • @Prady See e.g. stackoverflow.com/questions/11821261/…. – Keith C Mar 4 '16 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 ? – Prady Mar 4 '16 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.