0

Trying to render the list on visualforce page and convert multiselect picklist into dropdown instead of getting selectoptions from controller.

 Class returntoVF {
        String name;
        object someOtherObject;
    }

someotherobject has a multiselect picklist Fieldname (abc) and lets say values selected in this multiselect picklist are a;b;c;

List<returntoVF> is sent to VF page and apex:repeat is used as follows:

<apex:repeat value="{!returntoVF_List}" var="s">
    <apex:selectList size="1">
        <apex:selectoptions value="s.someOtherObject.abc.split(';')" />  <!--Looking to render this list as dropdown-->
    </apex:selectList>
</apex:repeat>

2 Answers 2

1

You need to convert it to a List<SelectOption>:

public List<SelectOption> options { get; set; }
public ReturnToVf()
{
    // one place to put this code would be in your constructor
    options = new List<SelectOption>();
    for (String option : abc.split(';'))
    {
        options.add(new SelectOption(option, option));
    }
}

Then you would just bind the value to this list:

<apex:selectOptions value="{!options}" />
6
  • Thanks Adrian for this but I am trying to think of ways to avoid apex here .
    – sf_user
    May 31, 2017 at 19:32
  • Pretty sure it can't be done with just markup.
    – Adrian Larson
    May 31, 2017 at 19:33
  • I was using APEX earlier but thinking of ways if it can be achieved directly in VF. I expeimented with split and some string functions but was not able to achieve so posted question here.
    – sf_user
    May 31, 2017 at 19:35
  • 1
    Yeah, I'm telling you it can't be done. Maybe someone will prove me wrong...
    – Adrian Larson
    May 31, 2017 at 19:46
  • 1
    @sf_user Visualforce doesn't have a way to express things like splitting strings. If returnToVF were a List of String, you could probably get away with it, but you definitely must split in Apex Code.
    – sfdcfox
    May 31, 2017 at 20:35
0

I tried using only visualforce but was not able to achieve however, here is something I figured out works for me and is completely javascript and html:

<select name="options" id="options">
    <script>
        var select = document.getElementById( 'options' );
        var option;
        var input = '{!s.someOtherObject.abc}'; 
        input.split(';').forEach(function(item) {
            option = document.createElement( 'option' );
            option.value = option.textContent = item;
            select.appendChild(option);
        });
    </script>
</select>
3
  • 1
    But then you have to arrange for a hidden variable elsewhere, because the select list won't be tied to any real VF binding.
    – sfdcfox
    May 31, 2017 at 20:23
  • yeah.. thats the downside i guess..
    – sf_user
    May 31, 2017 at 20:27
  • 1
    Also you need to escape any string you use in Javascript.
    – Adrian Larson
    May 31, 2017 at 20:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .