1

Is it possbile to have a prepopulated pick list inside of a form with values that the customer can chose and trigger the apex code with that. Currently I have a input text value. The user will type in the number that will be then used in a method trigered by the command button. I want to have a pick list with values from 1 - 10 that will do exactly the same.

<apex:inputtext value="{!userinput}" />

<apex:commandButton value="Generatethenum" action="{!genumber}" status="status" styleClass="btn btn-default" rerender="frm"/>

2 Answers 2

1

Yes, this can be done. And depending on what values you need there are 2 ways to handle this.

  1. Define picklist values in visualforce, using SelectList with SelectOption.
<apex:page controller="chooseColor">
    <apex:form>
        <apex:selectList id="chooseColor" value="{!userinput}" size="1">
            <apex:selectOption itemValue="1" itemLabel="1"/>
            <apex:selectOption itemValue="2" itemLabel="2"/>
            <apex:selectOption itemValue="3" itemLabel="3"/>
        </apex:selectList> 
        <apex:commandButton value="Generatethenum" action="{!genumber}" status="status" styleClass="btn btn-default" rerender="frm"/>
    </apex:form>
</apex:page>
  1. Define picklist values in apex controller, using SelectList with SelectOption.

Page

<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectList value="{!userinput}" >
            <apex:selectOptions value="{!numbers}"/>
        </apex:selectList><p/>

        <apex:commandButton value="Generatethenum" action="{!genumber}" status="status" styleClass="btn btn-default" rerender="frm"/>
    </apex:form>
</apex:page>

Controller:

public class sampleController {

    public List<SelectOption> getNumbers() {
        List<SelectOption> options = new List<SelectOption>();
        for(Integer i=1;i<11;i++){
           options.add(new SelectOption(String.valueOf(i),String.valueOf(i)));
        }
        return options;
    }
}
0

You absolutely can, and it's a much better way of taking input for this kind of thing than using free text.

You could list the values in the page itself, but it's a better idea to use the Apex controller to provide the options, as then you'll be defining them in the same piece of code that's checking them later on, as opposed to you having to maintain a list of values in both the page and the controller.

In your controller you just need a method that provides a list of SelectOption instances:

public List<SelectOption> getOperations()
{
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('1' 'Edit Users'));
    options.add(new SelectOption('2','Delete Users'));
    options.add(new SelectOption('3','Buy Beers for Users'));
    return options;
}

And then in the page you can write to a property on the controller as before:

<apex:selectList value="{!userinput}" title="Choose a Task">
    <apex:selectOptions value="{!Operations}"/>
</apex:selectList>

You'll probably want to check out the documentation for <apex:selectList> and you should probably consider making the values constant strings in your class to make maintenance easier and improve code readability. When you're checking the value of the userInput variable (which could be better named) this:

if(userInput == '1')

is a bit less descriptive than:

if(userInput == OP_EDIT_USERS)

You must log in to answer this question.