2

The Scenario: I have a selectlist on my page (1 value) with a list of options. I want it to default with an option.

My page:

<apex:page controller="mycontroller">
     <apex:selectlist value="{!SelectedItem}" size="1">
         <apex:selectOptions value="{Items}" />
     </apex:selectlist>
</apex:page>

Now I know in the controller I could have a string variable and a setter and a getter method which will work like this:

public class MyController{
     string S = 'Item1';
     public string getSelectedItem(){
          return s;
     }
     public void setSelectedItem(String s){
         this.s = s;
     }

     public list<SelectOption> getItems (){
         list<SelectOption> options = new list<SelectOption>();
         options.add(new SelectOption('','Select one'));
         options.add(new SelectOption('Item1','Item1'));
         //repeat
         return options;
     }
}

That will work it just seems... well like a lot of lines...

What I'm wonder is why doesn't something like this work:

public class MyController{
     public string SelectedItem {get;set;}
     public list<SelectOption> getItems (){
         list<SelectOption> options = new list<SelectOption>();
         options.add(new SelectOption('','Select one'));
         options.add(new SelectOption('Item1','Item1'));
         //repeat
         return options;
      }
     public MyController(){
        SelectedItem = 'Item1';
     }

I set the value of the string in the constructor instead of directly to the variable and collapsed the getter/setter... so why doesn't this work the same? If I want 'Item1' to be the defaulted value what else could I do?

7
  • In first snippet you have getSelectedItem() but setString(). Is this an anonymisation typo? Because technically these can't be collapsed into 1 {get;set;}. Also comma in list<SelectOption>, options.
    – eyescream
    Nov 7, 2012 at 21:41
  • The second snippet works for me, except there was an extra comma in this line, which was fine once updated list<SelectOption> options = new list<SelectOption>(); What isn't working? Nov 7, 2012 at 21:45
  • eyescream - Ya set string should be setSelectedItem bad typing. thanks for pointing out my two typos:) I'll fix. Nov 7, 2012 at 21:55
  • BritishBoyinDC - the second Snippet works works for you? It doesn't for me. I see the select list but my default of "Item1" doesn't display. Nov 7, 2012 at 21:56
  • Yes works alright, displays 'Item1' Nov 7, 2012 at 22:04

1 Answer 1

5

You're example worked for me. You can chop down the lines a bit more if you want by initializing the variable in the same line (which is nice IMHO). Another thing you may want to note is you can disable a select option if you want to have a start "Select Stuff" text that isn't itself selectable.

public class MyController{

  private static final String OPT1 = 'Item1';
  private static final String OPT2 = 'Item2';
  private static final String START = 'Select One';

  public String selectedItem { get; set; } { selectedItem = OPT1; }

  public List<SelectOption> options { get; private set; }
  {
    options = new List<SelectOption>();
    options.add(new SelectOption(START, START, true)); // ==> option disabled
    options.add(new SelectOption(OPT1, OPT1));
    options.add(new SelectOption(OPT2, OPT2));
  }

}
3
  • 1
    That's really cool. I still haven't gotten my own snippet to work facepalm I selected your answer because not only do you tell me my snippet works, but I learned two new things. I didn't know I could set the variable in the same line like that. THAT IS COOL. I also didn't know about the disable "start" option. That's even cooler! Thanks! Nov 8, 2012 at 14:18
  • Glad I could help ;) Nov 8, 2012 at 16:43
  • And in theory, if you did use a disabled "--Select Option--" value you'd probably want to default the selectedItem to that initially. Nov 8, 2012 at 16:44

You must log in to answer this question.

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