Activity Forums Salesforce® Discussions How to show PickList field as Radio Buttons on Visualforce Page?

  • Shekhar Gadewar

    Member
    July 22, 2016 at 9:16 am

    Hi Tanu,

    Try this>>>

    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;

    }

    }

  • Sourabh

    Member
    July 25, 2016 at 12:17 pm

    Hi Tanu,
    Try this code , Hope so it will be helpful for you..
    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('picklistField__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.picklistField__c}">
    <apex:selectoptions value="{!types}"></apex:selectoptions>
    </apex:selectRadio>
    Here picklistField__c is a field name whhich contains picklist values..

    Thanks

  • Parul

    Member
    September 12, 2018 at 4:53 pm

    Hi Tanu,
    Try this code , Hope so it will be helpful for you..
    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(‘picklistField__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.picklistField__c}”>
    <apex:selectoptions value=”{!types}”></apex:selectoptions>
    </apex:selectRadio>
    Here picklistField__c is a field name which contains picklist values..

    Thanks

  • shariq

    Member
    September 14, 2018 at 1:56 am

    Hi,

    Controller - 

    public class accEx {

    public accEx(ApexPages.StandardController controller) { }

    public List<SelectOption> getTypes(){
    Schema.sObjectType sobject_type = Account.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('Active__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;
    }
    }

    vf page - 

    <apex:page standardController="Account" extensions="accEx">
    <apex:form>
    <apex:selectRadio value="{!Account.Active__c}">
    <apex:selectOptions value="{!Types}"></apex:selectOptions>
    </apex:selectRadio>
    </apex:form>
    </apex:page>

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs