function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shree KShree K 

How to Add radio buttons to display selected records on the VF page

Hi 
I am trying to display Opportunity records based on their picklist value either "Closed Won" or "Closed Lost".
Upon selecting the radio button A all "Closed won" opportunities should be displayed,similarly
Upon selecting the radio button B all "Closed Lost " opportunities should be displayed.
so, in order to achieve this ,do i need to create radio buttons on my object or is is possible without creating radio buttons on the object.
i am unable to add radio buttons to my vf page when it has a pageblock table,

below is my VF Page Code for the reference,
please help me with the controller if required.

VF page:

<apex:page standardcontroller="Opportunity" tabstyle="Opportunity">
  <Apex:form >
  <Apex:pageblock Title="Records">
  <Apex:PageBlockTable value="{!Opportunity}" var="O">
   <Apex:Column value="{!O.Name}"/>
   <Apex:Column value="{!O.StageName}"/>
   <Apex:Column value="{!O.Amount}"/> 
   </Apex:PageBlockTable>
  </Apex:pageblock>
  </Apex:form>
</apex:page> 

Thanks in advance
Bhanu MaheshBhanu Mahesh
Hi CkRdY,

We can add radio buttons in the vF page itself and fetch the opportunities using custom controller or Extension to standard controller

Try the below code

VF page
<apex:page standardcontroller="Opportunity" tabstyle="Opportunity" extensions="OpportunitiesDisplay">
    <apex:form >
        <apex:pageblock >
            <apex:selectRadio value="{!selectedRadio}">
                <apex:selectOption itemLabel="Closed Won" itemValue="ClosedWon"/>
                <apex:selectOption itemLabel="Closed Lost" itemValue="ClosedLost"/>
                <apex:actionSupport event="onchange" action="{!getOpportunities}" reRender="records"/>
            </apex:selectRadio>
            <apex:outputPanel id="records">
                <apex:pageblockSection Title="Records">
                    <apex:PageBlockTable value="{!opps}" var="O">
                        <apex:Column value="{!O.Name}"/>
                        <apex:Column value="{!O.StageName}"/>
                        <apex:Column value="{!O.Amount}"/> 
                    </apex:PageBlockTable>
                </apex:pageblockSection>
            </apex:outputPanel>
        </apex:pageblock>
    </apex:form>
</apex:page>

Extension Class
public class OpportunitiesDisplay {

    public List<Opportunity> opps{get;set;}
    public String selectedRadio{get;set;}

    public OpportunitiesDisplay(ApexPages.StandardController controller) {
    }
    
    public PageReference getOpportunities(){
        opps = new  List<Opportunity>();
        if(selectedRadio == 'ClosedWon'){
            opps = [SELECT Id,Name,StageName,Amount FROM Opportunity WHERE IsClosed = true AND IsWon = true];
        }
        else{
            opps = [SELECT Id,Name,StageName,Amount FROM Opportunity WHERE IsClosed = true AND IsWon = false];
        }
        return null;   
    }
}

Mark this as "SOLVED" if your query is answered.

Thanks & Regards,
Bhanu Mahesh