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
Matias Ramirez 2Matias Ramirez 2 

Change checkbox by radioButton in a VFP

Hello everyone.

I need to process a selected row in a table in a VFP, let's say I need to show what is selected.

I can do it with Checkboxes, but I need to do it with RadioButtons (only one row should be checked at time)
I wanted to know if it is possible to achive this with Apex.

This is what I've done:
VFP
<apex:page controller="showContactsCtrl">
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockTable value="{!contactsWrapList}" var="contactVariable" >
                <apex:column onclick="{!contactVariable.selected}">
                    <apex:inputCheckbox value="{!contactVariable.selected}" id="CheckBox" onclick="SelectedOne(this)" > 
                    </apex:inputCheckbox>
                </apex:column>
                <apex:column value="{!contactVariable.c.name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:commandButton action="{!m1}" value="Select Contact" />
    </apex:form>
    
    <apex:outputPanel>
        <p>You have selected:</p>
        <apex:dataList value="{!selectedContact}" var="c">{!c}</apex:dataList>
    </apex:outputPanel>
</apex:page>

And this is the controller:
public class showContactsCtrl {
    public List<Contact> contacts;
    public List<contactsWrap> contactsWrapList {get; set;}
    public String selectedContact {get; set;}
    
    public showContactsCtrl(){
        contacts = [SELECT Id, Name FROM Contact WHERE Account.Id = someAccountId];
        contactsWrapList = new list<contactsWrap>();
        for(Contact c : contacts){
            contactsWrapList.add(new contactsWrap(false, c));
        }
    }
    
    public void m1(){
        for(contactsWrap contactsWrapObj : contactsWrapList) {
            if(contactsWrapObj.selected)
                selectedContact = contactsWrapObj.c.Name;
        }
    }
    
    
    public class contactsWrap{
        public Contact c {get; set;}
        public boolean selected{get; set;}
        
        public contactsWrap(Boolean s, Contact param){
            this.c = param;
            selected = s;
        }
    }
    
}

 
Best Answer chosen by Matias Ramirez 2
Khan AnasKhan Anas (Salesforce Developers) 
Hi Matias,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://readsalesforce.wordpress.com/2018/04/12/radio-button-with-records-in-pageblocktable-visualforce-page-salesforce-com-basics/

https://developer.salesforce.com/forums/?id=9060G000000I3vJQAS

http://www.infallibletechie.com/2012/07/radio-button-in-salesforce.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Matias,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://readsalesforce.wordpress.com/2018/04/12/radio-button-with-records-in-pageblocktable-visualforce-page-salesforce-com-basics/

https://developer.salesforce.com/forums/?id=9060G000000I3vJQAS

http://www.infallibletechie.com/2012/07/radio-button-in-salesforce.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Matias,
Please try the below code and let me know if this works for you. If still need modifications do let me know.

VF Page :

<apex:page controller="showContactsCtrl">
    <apex:slds />
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockTable value="{!contactsWrapList}" var="contactVariable" >
                <apex:column onclick="{!contactVariable.selected}">
                    <input type="radio" value="{!contactVariable.selected}" id="CheckBox" name="rasio" onclick="SelectedOne(this)" /> 
                    
                </apex:column>
                <apex:column value="{!contactVariable.c.name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:commandButton action="{!m1}" value="Select Contact" />
    </apex:form>
    
    <apex:outputPanel>
        <p>You have selected:</p>
        <apex:dataList value="{!selectedContact}" var="c">{!c}</apex:dataList>
    </apex:outputPanel>
</apex:page>

Controller :

public class showContactsCtrl {
    public List<Contact> contacts{get;set;}
    public List<contactsWrap> contactsWrapList {get; set;}
    public String selectedContact {get; set;}
    
    public showContactsCtrl(){
        contacts = [SELECT Id, Name FROM Contact WHERE Account.Id =: '0010o00002CgszDAAR'];
        contactsWrapList = new list<contactsWrap>();
        for(Contact c : contacts){
            contactsWrapList.add(new contactsWrap(false, c));
        }
    }
    
    public void m1(){
        for(contactsWrap contactsWrapObj : contactsWrapList) {
            if(contactsWrapObj.selected)
                selectedContact = contactsWrapObj.c.Name;
        }
    }
    
    
    public class contactsWrap{
        public Contact c {get; set;}
        public boolean selected{get; set;}
        
        public contactsWrap(Boolean s, Contact param){
            this.c = param;
            selected = s;
        }
    }
    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi Matias,
Greetings to you!

- I read your problem and implemented in my Org.
- Please use the below code [Solved] : -
VF page : -
      
<apex:page controller="displayContact" id="pg">
        <apex:form  id="frm">
            <apex:pageBlock id="pgblk" >
                <apex:pageBlockTable value="{!conList}" var="con" rows="100">
                    <apex:column width="10px">
                        <input type="radio" name="group1" />
                        <apex:actionSupport event="onclick" action="{!dispalyContact}" ReRender="conpgblk" >
                            <apex:param assignTo="{!conId}" name="conname" value="{!con.id}"/>
                        </apex:actionSupport>
                    </apex:column>
                    <apex:column value="{!con.Name}" />
                    <apex:column value="{!con.LastName}" />
                </apex:pageBlockTable>
            </apex:pageBlock>
            <p>You have selected:</p>
            <apex:pageBlock id="conpgblk" >
                <apex:outputPanel rendered="{!conList1.size == 0}">
                    <b> NO RELATED CONTACTS is selected.</b>
                </apex:outputPanel>
                <apex:outputPanel rendered="{!conList1.size != 0}">
                    <apex:pageBlockTable value="{!conList1}" var="con">
                        <apex:column value="{!con.Name}" />
                        <apex:column value="{!con.LastName}" />
                    </apex:pageBlockTable>
                </apex:outputPanel>
            </apex:pageBlock>
        </apex:form>
        </apex:page>


Class : - 

 
public with sharing class displayContact {

        public list<Contact> conList{get;set;}
        public list<Contact> conList1{get;set;}
        public String conName{get;set;}
        public String conId{get;set;}
        
        public displayContact(){
            conList=[SELECT Id,Name,AccountId,LastName FROM Contact LIMIT 10];

        }
        public PageReference dispalyContact() {
            if(conId != null) {
                conList1 = [SELECT id,Name ,FirstName, LastName FROM Contact WHERE Id=:conId Limit 1];
                if (conList1 != null){
                    conName = conList1.get(0).Name + '' + conList1.get(0).LastName;
                }

            }
            return null;
        }


    }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.