1

I am using a picklist on vf page, i am putting option values on the vf page. Is there any way to put values in picklist using my class ? vf page :

<apex:page controller="myclass">
<apex:form>
<apex:pageblock>
    <apex :pageblocksection>
    <apex:selectList value="{!selected}">
        <apex:selectOption itemValue="+" itemLabel="+"/>
        <apex:selectOption itemValue="-" itemLabel="-"/>
        <apex:selectOption itemValue="/" itemLabel="/"/>
        <apex:selectOption itemValue="*" itemLabel="*"/>
        </apex:pageblocksection
        </apex:pageblock>
    </apex:selectList> 
</apex:form>

3 Answers 3

3

You can create a list on your controller. And then pass that list on the vf page like this :

public List<SelectOption> getmyOptions()
    {
    List<SelectOption> selectOptionList=new List<SelectOption>();
        selectOptionList.add(new SelectOption('+','+'));
        selectOptionList.add(new SelectOption('-','-'));
        selectOptionList.add(new SelectOption('/','/'));
        selectOptionList.add(new SelectOption('*','*'));
        return selectOptionList;
    }

And on the vf page you can use this as :

 <apex:selectList value="{!selected}">
        <apex:selectOptions value="{!myOptions}"/>
    </apex:selectList>

Let me know if this helps. you can use this link to know more about selectlist : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectOption.htm

1

MyClass :

public String fields { get; set; }
       List<SelectOption> optns=new List<SelectOption>();
        public list<selectoption> getvalues(){
           optns.add(new SelectOption('FirstName','FirstName'));
            optns.add(new SelectOption('LastName','LastName'));
            optns.add(new SelectOption('Email','Email'));
            optns.add(new SelectOption('Phone','Phone'));                        
            return optns;
        }

page:

       <apex:selectList value="{!fields}" multiselect="true" size="3">
            <apex:selectOptions value="{!values}"/>
        </apex:selectList>
1

Sample code:

<apex:selectOptions value="{!OptionList}"/>

Controller:

public List<SelectOption> getOptionList() 
{
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('+', '+');
    options.add(new SelectOption('-', '-');
    return options;
}

You must log in to answer this question.

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