+ Start a Discussion
TryCodingTryCoding 

How to show PickList field as Radio Buttons on Visualforce Page

Hi all

i am having a problem and not able to get the solution even after a lot of serching

 

I have a picklist field with values 'yes' and 'No'

 

I want to show this field as radio buttons on my vf page. i.e. one radio button for yes and one for no. and at a time only one can be selected

 

can anybody help me how to do this...

 

i have tried using method on link below

http://blog.sforce.com/sforce/2008/12/using-the-metadata-api-to-retrieve-picklist-values.html 

 

but this is giving error.. please suggest alternate solution..

thanks in advance..

Best Answer chosen by Admin (Salesforce Developers) 
TryCodingTryCoding

hi pradeep

i think you did not understand my question...

yes,No is in my picklist field.that could be apple,mango,banana or whatever

At time of creting my UI (Visualforce Page) i know that i have a picklist field but i dont know the values in it.

i want to first retreive those values and then show them on my VF page as radio Buttons

 

Fortunately i have found solution to this. And i am posting that here.

Note that type__c is my picklist field.

 

Use following methos to get options for radio button on page

controller code:

 

public List<SelectOption> getTypes(){
            Schema.sObjectType sobject_type = customObject__c.getSObjectType();


            Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();


            Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();
          
            List<Schema.PicklistEntry> pick_list_values = field_map.get('type__c').getDescribe().getPickListValues();


            List<selectOption> options = new List<selectOption>();


           for (Schema.PicklistEntry a : pick_list_values) {
                      options.add(new selectOption(a.getLabel(), a.getValue()));
          }
      return options;

 

to use this on page:

 

<apex:selectRadio value="{!customObject__c.Type__c}">
                    <apex:selectoptions value="{!types}"></apex:selectoptions>
 </apex:selectRadio>


I hope this will help solving someone else's problem.

cheers,

TryCoding :)

All Answers

Pradeep_NavatarPradeep_Navatar

Find below a sample code :

 

 VF CODE:

 

                <apex:page controller="ProductSelectionlist" >

                                <apex:form>

                                <apex:pageblock title="Product Selection" id="theBlock">

                                       <apex:outputtext value="Which product would you like to add to the oppoortunity? (select one)" />

                                                      <apex:pageblocksection columns="1">

                                                                 <apex:SelectRadio value="{!ProductOption}">

                                                                                <apex:selectOptions value="{!yesno}"/>

                                                                </apex:SelectRadio>

                                                      </apex:pageblocksection>

                                </apex:pageblock>

                            </apex:form>

            </apex:page>

 

    CONTROLLER CODE :

 

                public class ProductSelectionlist

                {

                 Public string ProductOption{get;set;}

                 Public ProductSelectionlist(){}

                 public List<SelectOption> getYesNo() {

                List<SelectOption> options = new List<SelectOption>();

                 options.add(new SelectOption('true', 'Yes'));

                 options.add(new SelectOption('false', 'No'));

                return options;

                  }

                }

TryCodingTryCoding

hi pradeep

i think you did not understand my question...

yes,No is in my picklist field.that could be apple,mango,banana or whatever

At time of creting my UI (Visualforce Page) i know that i have a picklist field but i dont know the values in it.

i want to first retreive those values and then show them on my VF page as radio Buttons

 

Fortunately i have found solution to this. And i am posting that here.

Note that type__c is my picklist field.

 

Use following methos to get options for radio button on page

controller code:

 

public List<SelectOption> getTypes(){
            Schema.sObjectType sobject_type = customObject__c.getSObjectType();


            Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();


            Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();
          
            List<Schema.PicklistEntry> pick_list_values = field_map.get('type__c').getDescribe().getPickListValues();


            List<selectOption> options = new List<selectOption>();


           for (Schema.PicklistEntry a : pick_list_values) {
                      options.add(new selectOption(a.getLabel(), a.getValue()));
          }
      return options;

 

to use this on page:

 

<apex:selectRadio value="{!customObject__c.Type__c}">
                    <apex:selectoptions value="{!types}"></apex:selectoptions>
 </apex:selectRadio>


I hope this will help solving someone else's problem.

cheers,

TryCoding :)

This was selected as the best answer
Milan SanghaniMilan Sanghani

How to write Test class for this method???

Umesha BP 9Umesha BP 9
Hi @TryCoding ,All
please send VF code Also..
Harsh ShendeHarsh Shende
Hi Pradeep,
     Thanks for the code. It works Great, But One Question, Instead of Yes No , If I have 5 values in my Picklist such as MIN, MOD, MAX, DEP ,TOTAL. How I can do it. 
Sai Bharathwaj SureshbabuSai Bharathwaj Sureshbabu
Hi Pradeep, 
If I have five more fields . How can i do that