+ Start a Discussion
raj jordanraj jordan 

how to display objects in radio button?

Hi am trying to display account and contact object as radio buttons 
when i click on account its record should display and
when i click on contact its record should display 
but am not getting the required output 
Below is my code 
where it is only displaying contact details 
please tell me where am going wrong
 
<apex:page controller="radio16">
    <apex:form >
        <apex:pageblock >
            <apex:selectRadio value="{!selectedRadio}">
                <apex:selectOption itemLabel="Account" itemValue="Accounts"/>
                <apex:selectOption itemLabel="Contact" itemValue="Contacts"/>
                <apex:actionSupport event="onclick" action="{!getDetails}" reRender="records"/>
            </apex:selectRadio>
            <apex:outputPanel id="records">
                <apex:pageblockSection Title="Records">
                    <apex:PageBlockTable value="{!acc}" var="a">
                        <apex:Column value="{!a.Name}"/>
                       
                    </apex:PageBlockTable>
                     <apex:PageBlockTable value="{!con}" var="c">
                        <apex:Column value="{!c.lastname}"/>
                       
                    </apex:PageBlockTable>
                </apex:pageblockSection>
            </apex:outputPanel>
        </apex:pageblock>
    </apex:form>
</apex:page>
 
public class radio16 {

    public String selectedRadio { get; set; }


    public List<Account> acc{get;set;}
    public List<Contact> con{get;set;}
   

   
    
    public PageReference getDetails(){
        acc = new  List<Account>();
        if(selectedRadio== 'Account'){
            acc = [SELECT name FROM Account limit 5];
        }
        else{
        con =new List<Contact>();
            con = [SELECT lastname FROM Contact limit 5];
        }
        return null;   
    }
}

 
Best Answer chosen by raj jordan
Vinod ChoudharyVinod Choudhary
Hi Raj,

Try this:

Class:
public class radio16 {

    public String selectedRadio { get; set; }


    public List<Account> acc{get;set;}
    public List<Contact> contact{get;set;}
   

   public radio16() {
       getDetails();
    }  
    
    public void getDetails(){
    
        acc = new  List<Account>();
        if(selectedRadio== 'Accounts'){
            acc = [SELECT name FROM Account limit 5];
            contact.clear();
        }
        else{
        contact=new List<Contact>();
            contact= [SELECT lastname FROM Contact limit 5];
            acc.clear();
        }
   
    }
}

VF page:
 
<apex:page controller="radio16">
    <apex:form >
        <apex:pageblock >
            <apex:selectRadio value="{!selectedRadio}">
                <apex:selectOption itemLabel="Account" itemValue="Accounts"/>
                <apex:selectOption itemLabel="Contact" itemValue="Contacts"/>
                <apex:actionSupport event="onclick" action="{!getDetails}" reRender="records"/>
            </apex:selectRadio>
            <apex:outputPanel id="records">
                <apex:pageblockSection Title="Records">
                <apex:pageBlockTable rendered="{!acc.size!=0}" value="{!acc}" var="a">
                   
                        <apex:Column value="{!a.Name}"/>
                       
                    </apex:PageBlockTable>
                     <apex:PageBlockTable rendered="{!contact.size!=0}" value="{!contact}" var="c">
                        <apex:Column value="{!c.id}"/>
                       
                    </apex:PageBlockTable>
                </apex:pageblockSection>
            </apex:outputPanel>
        </apex:pageblock>
    </apex:form>
</apex:page>

Thanks
Vinod

All Answers

Vinod ChoudharyVinod Choudhary
Hi Raj,

Try this:

Class:
public class radio16 {

    public String selectedRadio { get; set; }


    public List<Account> acc{get;set;}
    public List<Contact> contact{get;set;}
   

   public radio16() {
       getDetails();
    }  
    
    public void getDetails(){
    
        acc = new  List<Account>();
        if(selectedRadio== 'Accounts'){
            acc = [SELECT name FROM Account limit 5];
            contact.clear();
        }
        else{
        contact=new List<Contact>();
            contact= [SELECT lastname FROM Contact limit 5];
            acc.clear();
        }
   
    }
}

VF page:
 
<apex:page controller="radio16">
    <apex:form >
        <apex:pageblock >
            <apex:selectRadio value="{!selectedRadio}">
                <apex:selectOption itemLabel="Account" itemValue="Accounts"/>
                <apex:selectOption itemLabel="Contact" itemValue="Contacts"/>
                <apex:actionSupport event="onclick" action="{!getDetails}" reRender="records"/>
            </apex:selectRadio>
            <apex:outputPanel id="records">
                <apex:pageblockSection Title="Records">
                <apex:pageBlockTable rendered="{!acc.size!=0}" value="{!acc}" var="a">
                   
                        <apex:Column value="{!a.Name}"/>
                       
                    </apex:PageBlockTable>
                     <apex:PageBlockTable rendered="{!contact.size!=0}" value="{!contact}" var="c">
                        <apex:Column value="{!c.id}"/>
                       
                    </apex:PageBlockTable>
                </apex:pageblockSection>
            </apex:outputPanel>
        </apex:pageblock>
    </apex:form>
</apex:page>

Thanks
Vinod
This was selected as the best answer
raj jordanraj jordan
thanks vinod its working