As the title of the article implies, in this post we will be seeing an example which uses radio button in a table in visualforce and allows selecting one of the records in the table.
You can see a demo of the example here - Click here (forcetreedemos)
Visualforce Page:
Apex Class: RadioButton
You can see a demo of the example here - Click here (forcetreedemos)
Visualforce Page:
<apex:page controller="RadioButton" showheader="false">
<apex:form>
<apex:pageblock id="allcons" title="Available Contacts">
<apex:pageblocktable id="allcons" value="{!AllContacts}" var="allcon">
<apex:column headervalue="Set as Primary">
<apex:actionsupport action="{!selectcon}" event="onclick" rerender="consel,allcons">
<input type="radio" />
<apex:param name="conid" value="{!allcon.Id}">
</apex:param></apex:actionsupport>
</apex:column>
<apex:column headervalue="Last Name">
<apex:outputfield value="{!allcon.LastName}">
</apex:outputfield></apex:column>
<apex:column headervalue="First Name">
<apex:outputfield value="{!allcon.FirstName}">
</apex:outputfield></apex:column>
<apex:column headervalue="Email">
<apex:outputfield value="{!allcon.Email}">
</apex:outputfield></apex:column>
<apex:column headervalue="Phone">
<apex:outputfield value="{!allcon.Phone}">
</apex:outputfield></apex:column>
</apex:pageblocktable>
</apex:pageblock>
<apex:pageblock id="consel" title="Selected Contact">
<apex:pageblocktable id="allcons" value="{!selectedContact}" var="selcon">
<apex:column headervalue="Last Name">
<apex:outputfield value="{!selcon.LastName}">
</apex:outputfield></apex:column>
<apex:column headervalue="First Name">
<apex:outputfield value="{!selcon.FirstName}">
</apex:outputfield></apex:column>
<apex:column headervalue="Email">
<apex:outputfield value="{!selcon.Email}">
</apex:outputfield></apex:column>
<apex:column headervalue="Phone">
<apex:outputfield value="{!selcon.Phone}">
</apex:outputfield></apex:column>
</apex:pageblocktable>
</apex:pageblock>
</apex:form>
</apex:page>
Apex Class: RadioButton
public class RadioButton {
List<contact> selectcon;
Public List<contact> getAllContacts()
{
List<contact> allcons = [Select Id,FirstName,LastName,Email,Phone from Contact limit 5];
return allcons;
}
Public void selectcon()
{
String selcontactid = System.currentPagereference().getParameters().get('conid');
Contact con = [Select Id,FirstName,LastName,Email,Phone from Contact where Id=:selcontactid];
selectcon = new List<contact>();
selectcon.add(con);
}
Public List<contact> getselectedContact()
{
return selectcon;
}
}