Find your content:

Search form

You are here

Dynamically assign value field for an apex:selectList

 
Share

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


Attribution to: Owen Davies

Possible Suggestion/Solution #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.


Attribution to: jordan.baucke

Possible Suggestion/Solution #2

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.


Attribution to: JCD
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/979

My Block Status

My Block Content