5

I have created a custom component to represent selectLists. This allows them to be defined by a non-tech user to generate custom VF pages.

My question is can you dynamically set the value attribute for a select list? I have two fields in the controller. One for single selects that is of type string and one for multi selects that is of type List.

Problem is I can only set the value field to be one or the other. I want to be able to do something like the following. The formula field in the value attribute is invalid for the obvious reason Component code:

<apex:attribute name="multiSelect"
     type="Boolean"
     assignTo="{!multiSelect}"
     required="true"
     description="Indicates if the drop down is multiSelect."
 />
 <apex:selectList id="selector"
     value="{!IF(multiSelect, multiValues, multiValue)}"
     multiselect="{!multiSelect}"
     size="3" 
     title="A title"
>

Controller code

public Boolean multiSelect { get; set; }
public List<String> multiValues { get; set; }
public String mulitValue { get; set; }

Cheers

4

It's not possible to dynamically choose which controller property you want to bind a VF component to, but with a bit of extra markup you can dictate which property is set:

<apex:attribute name="multiSelect"
    type="Boolean"
    assignTo="{!multiSelect}"
    required="true"
    description="Indicates if the drop down is multiSelect."
/>
<apex:selectList
    value="{!multiValues}"
    multiselect="true"
    rendered="{!multiSelect}"
>
    <apex:selectOptions value="{!selectOpts}" />
</apex:selectList>
<apex:selectList
    value="{!singleValue}
    multiselect="false"
    rendered="{!NOT(multiSelect)}"
>
    <apex:selectOptions value="{!selectOpts}" />
</apex:selectList>

Then determining which variable to use in the rest of your controller is as simple as checking the value of your multiSelect boolean.

| improve this answer | |
  • Simple usually works best. Cheers – Owen Davies Aug 29 '12 at 16:32
1

I think the multiselect possible selection values will be set when the page is loaded. It's not possible to 'dynamically' set them, however, it is possible to build up some logic to display a different set of values depending on some parameter passed into the component.

It maybe also possible to set them dynamically with javascript, or a partial refresh using an <apex:actionFunction> to change the values based on some arg. and re-render that component.

| improve this answer | |
  • 1
    Yep the javascript work around is an option. I was just wondering if it was required as being able to choose the controller binding based on a formula or something like that would be helpful in this case. Cheers for the response – Owen Davies Aug 29 '12 at 15:21

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.