3

my picklist value comes from an object but upon page load, i want to have '--select student--' as the default value of the picklist.

controller snippet

public List<selectOption> getsNames(){

        List<selectOption> options = new List<selectOption>(); 
        options.add(new selectOption('', '--Select Student--'));
        for(Parent__c p : [Select Id, Name, First_Name__c, (Select Id, First_Name__c, Name from Students__r) 
                           from Parent__c WHERE Name = 'Delgado']){
                               for(Student__c stud : p.Students__r){
                                   selectedSId = stud.Id;    
                                   options.add(new selectOption(stud.Id, stud.First_Name__c + ' ' + stud.Name));
                               }
                           }
        return options;

    }

in this code, the '--select student--' appears on the top but on page load, the name of the student appears. how can i make it a default that '--select student--' is the value of the picklist?

VFP

    <apex:pageBlock id="chooseStudentPageBlock" >            
<div>    
<apex:outputLabel style="margin-left: 55px;">Select Student:</apex:outputLabel>
<apex:actionRegion>
   <apex:selectList size="1" style="margin-left: 5px;" value="{!selectedSId}" >
         <apex:actionSupport event="onclick"   reRender="teacher"/>
                <apex:selectOptions value="{!SNames}"></apex:selectOptions>
                      <apex:actionSupport event="onchange"  action="{!fetchTeacherInfo}" reRender="teacherBlock"/>
     </apex:selectList>
    </apex:actionRegion>
 </div>                 
 </apex:pageBlock> 
  • have you tried adding javascript on pageload ? – Rainmaker Feb 24 '16 at 3:29
  • no. is there any other way? – sfdclearner Feb 24 '16 at 3:33
  • have you tried putting options.add(new selectOption('', '--Select Student--')); before return options; – Micz Domz Feb 24 '16 at 10:11
3

You can simply default by assigning the default value to your "selectedSId" variable in your class constructor.Try the below code

   public classconstructor(){ //This is your class constructor
      getsNames();
      selectedSId = ''; 
   }
   public List<selectOption> getsNames(){

    List<selectOption> options = new List<selectOption>(); 
    options.add(new selectOption('', '--Select Student--'));
    for(Parent__c p : [Select Id, Name, First_Name__c, (Select Id, First_Name__c, Name from Students__r) 
                       from Parent__c WHERE Name = 'Delgado']){
        for(Student__c stud : p.Students__r){   
                options.add(new selectOption(stud.Id, stud.First_Name__c + ' ' + stud.Name));
        }
      }
    return options;

}
| improve this answer | |
0
<apex:outputText value="{!selectedSId}" style="display:none;"></apex:outputText>

returns the first value on the picklist. thank you

| improve this answer | |
  • Gelay.. No this is not an answer.. I really don't know why your code is not working what suppose to.. But for workaround I did this so for now your issue resolved – Ratan Paul Feb 24 '16 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.