+ Start a Discussion
KidNikiKidNiki 

Return label value to controller from SelectList/SelectOption...

Is it possible to return the label value of the selected item in a selectlist to the controller.  Sorry, Im new at this and couldn't find anything.

 

I just want to know what the label of the currently selected item is instead of the value (which I need different for something else)

 

Thank you

fgwarbfgwarb

If you're defining your options on the VisualForce page then you'll have to do something tricky to get it done.

 

If you're defining your options in the Apex Controller then you can derive the Label from the value.

 

 

Tricky VF Method:

 

Pseudo-code

VF page:
<apex:selectList size="1" value="{!controllerVar}">
 <apex:selectOption itemLabel="Option1" itemValue="Option1-A" />
 <apex:selectOption itemLabel="Option2" itemValue="Option2-B" />
 <apex:selectOption itemLabel="Option3" itemValue="Option3-C" />
</apex:selectList>



Controller:
String controllerVar{get;set}
public void yourAction(){
 String selectedOptionLabel = controllerVar.split('-')[0];
 String selectedOptionValue = controllerVar.split('-')[1];
}

 

Options "hardcoded" in Apex Controller:

Pseudo-code

VF Page:
<apex:selectList value="{!controllerVar}" size="1">
 <apex:selectOptions value="{!optionsFromController}" />
</apex:selectList>


Controller:
public class demo{
//overload the constructor to add default values into the optionsFromController variable
 public demo(){
  optionsFromController = new SelectOption[]{};
  optionsFromController.add(new SelectOption('A','Option1'));
  optionsFromController.add(new SelectOption('B','Option2'));
  optionsFromController.add(new SelectOption('C','Option3'));
 }
 SelectOption[] optionsFromController{get;set;}
 String controllerVar{get;set;}
 public void yourAction(){
  if(controllerVar == 'A'){
   //we know that it's Option1 because we coded that above
  }else if(controllerVar == 'B'){
   //we know that it's Option2 because we coded that above
  }else{
   //we know that it's Option3 because we coded that above, and it wasn't A or B
  }
 }

 

 

 

Or you could go an insane 3rd route and store your options and labels in a custom object in Salesforce and do SOQL queries against the table to load the values into the VF page, then store those values in a map and check the map in the yourAction method.

 

Hope this helps!

 

-g

KidNikiKidNiki

Im not, its populated via a soql query in the controller.  Im going to try and grab the value via query and post it back to the controller.  Ill post my findings here.  Thank you for your feedback, I didn't think it would be easy.

fgwarbfgwarb

Since you're working in the controller I think you'd be able to use a map to store the values for reference... something like:

 

public class controller(){
 Map<String, String> dict = new Map<String, String>();
 String selectedOption{get;set;}
 public SelectOption[] getOptions(){
  SelectOption[] opts = new SelectOption[];
  for(Account a : [SELECT Id, Name FROM Account]){
   opt.Add(new SelectOption(a.Id, a.Name);
   dict.put(a.Id, a.Name);
  }
 }

 public void yourAction(){
  String yourLabel = dict.get(selectedOption);
 }

 

You've cloned the values in the 

SFenthusiast82SFenthusiast82

You can have a variable in the controller to keep the label of the selected option NAME  just like you are keeping one to store the selected option ID. and you can instantiate it as follows:

 

public List<SelectOption> options {get; set;}
        public string Choice {get; set;}
        public string choiceName {get{
            for (SelectOption so : Options){
                if (so.getValue() == choice){
                    return so.getLabel();
                }
            }
            return null;    
        }}
        public String getChoice() {
            return Choice;
        }

 

This will give the selected option NAME / LABEL in the controller in choiceName variable.