function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Toasty02Toasty02 

Where is the value of a selectList stored?

Hello,
 
I'm new to this Apex stuff, so I'm hoping someone will be able to help me out.  I have an Apex page with a selectList, and I'd like to take the value that the use selects and do something with it on the page.  The code I have doesn't appear to be working, and I can't figure out why.
 
This is my controller class:
 
public class SimplePrintLabelsController {
public String selectedLabelTemplateName {get; set;}
public List<selectOption> labelTemplateList {
    get {
        List<selectOption> labelTemplateOptions = new List<selectOption>();
        labelTemplateOptions.add(new selectOption('', '--Select a label template--'));
       
        for (Label_Template__c lt : [select name from Label_Template__c order by name]) {
            labelTemplateOptions.add(new selectOption(lt.name, lt.name));
        }
       
        return labelTemplateOptions;   
    }
    set;
}
}
 
 
And here is the Apex page code:
 
<apex:page controller="SimplePrintLabelsController">
  <script type="text/javascript">
      function showAlert() {
          alert("You selected " + "{!selectedLabelTemplateName}");
      }
  </script>  
  <apex:form >
      <apex:pageBlock >
          <apex:outputLabel value="Label template: "/>
          <apex:selectList id="labelTemplates" value="{!selectedLabelTemplateName}" size="1" onChange="showAlert()">
              <apex:selectOptions value="{!labelTemplateList}"/>
          </apex:selectList>
      </apex:pageBlock>
      <apex:pageBlock >
          <apex:outputLabel value="You selected: {!selectedLabelTemplateName}"/>
      </apex:pageBlock>
  </apex:form>
</apex:page>
 
 
 
As you can see, I'm trying to display the selected value in both an output label and in a JavaScript alert.  However, the selected value is not showing up in either place.  Can anyone tell me what I'm doing wrong here?
 
Thanks very much!
 
- Carolyn
aalbertaalbert
I had a similar requirement in a past project. I solved it with the <apex:actionFunction> component. Below is the snippet. I specify an "onclick" event on the <apex:selectList> which invokes the actionFunction defined earlier. And I pass in the "this.value" variable which is set to the selectOption the user has selected.

Code:
<apex:actionFunction status="statusForListBucket" action="{!listBucket}" name="listBucket" rerender="listBucketResults">
            <apex:param name="firstParam" assignTo="{!bucketToList}" value="" /> 
</apex:actionFunction>
<apex:selectList onclick="listBucket(this.value)" value="{!bucketToList}" multiselect="false">
   <apex:selectOptions value="{!BucketNames}"/>
</apex:selectList><br/>
 

 

Toasty02Toasty02

Thanks for the reply.  I still can't quite put this together...what needs to happen in the controller class for this to work?  That is, what should the listBucket method look like?

 

Thanks,

Carolyn

aalbertaalbert
For starters, note the <apex:param> within the actionFunction. When that actionFunction executes, it sets the parameter/variable "bucketToList" in the controller.

So in my example, there is a simple controller variable, bucketToList, for example:

public String bucketToList {get;set;}

Next, there is also an action method defined in the controller class:

public PageReference listBucket(){

  ...do your logic here....but the bucketToList property is already set....
}



Toasty02Toasty02

Awesome - my output label is now displaying the name of the selected Label Template object.  I still can't get the JavaScript alert to display the name, but this still helps a lot.  Thanks again!

 

- Carolyn 

SriramAravindSriramAravind

Oh nice man. You helped me alot. with this nice example. Thanks for this.