+ Start a Discussion
Sivakumari2iSivakumari2i 

how to dispaly item selected in picklist in visualforce page

Hi,

 

This is my code,

 

<apex:page controller="picklistTest" showHeader="false" >
  <apex:form >
        <apex:pageBlock title="Sample">
           <apex:pageBlock title="Select the Quarter:">
           <apex:selectList value="{!selectedVal}" size="1">
           <apex:selectOptions value="{!options1}"/>
           </apex:selectList>
           </apex:pageBlock>

     </apex:pageBlock>
  </apex:form>
</apex:page>


I have some values in the picklist and once the user selects a value from the list i want to display that value (i.e) label in the same visualforce page in a seperate section.

Can anyone share some code to achieve this?

Please help me

 

Regards,

S.Sivakumar

 

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

You can't enclose apex:actionregion inside apex:selectlist. So try reverse.

 

<apex:page controller="picklistTest" showHeader="false" >
  <apex:form >
        
        <apex:pageBlock title="Sample">
               
               <apex:pageBlock title="Select Quarter :">
		   <apex:actionRegion >  
                       <apex:selectList value="{!selectedVal}" size="1">
                           <apex:selectOptions value="{!options1}"/>
                           <apex:actionSupport event="onchange" rerender="sample1"/>
                       </apex:selectList>
		   </apex:actionRegion>   
               </apex:pageBlock>
              
               <apex:PageblockSection title="sample" columns="1" id="sample1" >
               <apex:OutputLabel >Selected Period</apex:OutputLabel>
               {!selectedVal}
               </apex:PageblockSection>
        </apex:pageBlock>

  </apex:form>
</apex:page>

 

All Answers

Rahul SharmaRahul Sharma
In your option list(options1 list), whatever value you select will be set to selectedVal.
- So if you want the value on change of that picklist then use apex:actionsupport and rerender another section.
- if you want it on button click, then you could use command button to rerender the new another section.
Note: In your another section, you will be putting the selected value.
Sivakumari2iSivakumari2i

Hey thanks for your reply.

 

I changed my code as you mentioned.

 

but i am getting the following error

 

Visualforce Error


Select components should have at least one child component of type selectOption or selectOptions

 

Here is my code,

 

<apex:page controller="picklistTest" showHeader="false" >
  <apex:form >
        
        <apex:pageBlock title="Sample">
               
               <apex:pageBlock title="Select Quarter :">
                   <apex:selectList value="{!selectedVal}" size="1">
                       <apex:actionRegion >     
                            <apex:actionSupport event="onchange" rerender="sample1"/>
                             <apex:selectOptions value="{!options1}"/>
                       </apex:actionRegion>        
                   </apex:selectList>
               </apex:pageBlock>
              
               <apex:PageblockSection title="sample" columns="1" id="sample1" >
               <apex:OutputLabel >Selected Period</apex:OutputLabel>
               {!selectedVal}
               </apex:PageblockSection>
        </apex:pageBlock>
 
  </apex:form>
</apex:page>

Rahul SharmaRahul Sharma

You can't enclose apex:actionregion inside apex:selectlist. So try reverse.

 

<apex:page controller="picklistTest" showHeader="false" >
  <apex:form >
        
        <apex:pageBlock title="Sample">
               
               <apex:pageBlock title="Select Quarter :">
		   <apex:actionRegion >  
                       <apex:selectList value="{!selectedVal}" size="1">
                           <apex:selectOptions value="{!options1}"/>
                           <apex:actionSupport event="onchange" rerender="sample1"/>
                       </apex:selectList>
		   </apex:actionRegion>   
               </apex:pageBlock>
              
               <apex:PageblockSection title="sample" columns="1" id="sample1" >
               <apex:OutputLabel >Selected Period</apex:OutputLabel>
               {!selectedVal}
               </apex:PageblockSection>
        </apex:pageBlock>

  </apex:form>
</apex:page>

 

This was selected as the best answer
Sivakumari2iSivakumari2i

hey its working.

But i am getting id of the corresponding one. I want to display the label of it.

How to achieve this?

 

Regards,

S.Sivakumar

Rahul SharmaRahul Sharma
You could prepare map of Value vs the Label, then use that map for fetching value from selectedVal variable.
Sivakumari2iSivakumari2i

i dont know how to do it. can u help me with some code?

 

Regards,

S.Sivakumar

Rahul SharmaRahul Sharma

I have written some code, it might help you.

Page : 

<apex:page controller="bigselectList">
    <apex:form >
        <apex:selectList value="{!selectedCountryValue}" title="Choose a country">
            <apex:selectOptions value="{!items}"/>
        </apex:selectList><br/>
        <apex:commandButton value="Get picklist Label" action="{!showValue}" rerender="out" status="status"/>
    </apex:form>
    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel >
                    <p>You have selected:</p>
                    value - {!selectedCountryValue} : Label - {!selectedCountryLabel}
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:page>

--------------------------------------------------------------------
/*** Controller: ***/
public class bigselectList {
    // Below map will be used for storing the Labels and values of picklist
 	Map<String, String> mapSelectListValues = new Map<String, String>();
 	
 	public List<SelectOption> items		{get;set;}
 	
 	// This property will be used just for holding up the selected Value's Label
 	public String selectedCountryValue	{get;set;}
 	public String selectedCountryLabel	{get;set;}
 	
 	// Constructor
    public bigselectList() {
    	
    	// Filling the options of select list
    	items = new List<SelectOption>();
        items.add(new SelectOption('US1','US'));
        items.add(new SelectOption('CANADA1','Canada'));
        items.add(new SelectOption('MEXICO1','Mexico'));
        
        // For auto selecting the first value in visualforce page
        selectedCountryValue = items[0].getValue();
    	selectedCountryLabel = items[0].getLabel();
        
        // prepare map of select options
        for(Selectoption s: items) {
        	mapSelectListValues.put(s.getValue(), s.getLabel());
        }
    }
    
    public void showValue() {
    	// On click of button fetch the Label of selected Value from picklist
    	selectedCountryLabel = mapSelectListValues.get(selectedCountryValue);
    }
}

 

 

 Let us know if you need any help.

Sivakumari2iSivakumari2i

Heys its working,

Thanks a lot

Can u please explain me the concept?

 

Thanks,

S.Sivakumar

 

Rahul SharmaRahul Sharma
I have just used a map for holding up the options value and label.
Then on button click we can get the selected value from that map.
Refer my post again, I have added comments in appropriate places.
Sivakumari2iSivakumari2i

Similarly i have to  use the selected item id in the picklists and have to query to get another result based on that id.

I used same selectedval for that.but i am not able to get results (i.e)label from controller to visualforce page and i am able to dispaly the id of the query result.

 

I stored the results of the query in List<sobject> and i want ti get the label not the index.

 

Thanks,

S.Sivakumar

Rahul SharmaRahul Sharma
Could you post your code.
Sivakumari2iSivakumari2i

hey thanks,

I got the logic. I used map function to achieve that.

 

Right now i want to calculate day in a quarter(i.e) if user selects Quarter 1 2013 from the picklists then i want to display the current day in that quarter. Suppose if he selects the passed quarter then i want to display,"quarter completed". I dont know how to achieve this.

Please help me.

 

Regards,

S.Sivakumar