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
Saikishore Reddy AengareddySaikishore Reddy Aengareddy 

Search Results with Radio selection

Hello All-

 

I am trying to display list of records (Search Results) with radio button selection. We can use wrapper class to display list of records with checkboxes available for each record... Is it possible to have radiobutton in place of checkbox?  Can you please suggest me way to do it.. or any sample code...

 

Thanks.
 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

you can WRITE javascript on radio button.

 

Create an apex  hidden field , update this using javascript by id of selected contact record, you can get same in c.c.Id

 

Now access this hidden field in controller, mean Id of Contact

 

 

Thanks,

Bala

 

All Answers

bob_buzzardbob_buzzard

You'd use the same technique for radio buttons as checkboxes.  If the options are different for each row you'll need to encapsulate that in your wrapper class.  Aside from that you need a string to store the chosen value rather than a checkbox.

kiranmutturukiranmutturu

replace the select check box component with 

 

 

<apex:selectRadio value="{!country}">         

  <apex:selectOptions value="{!items}"/>         

  </apex:selectRadio>

 

but as you know here multi select is not possible,......

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Here is the controller and vf page that i am using... Can you please point me what needs to be modified... 

 

public class wrapperClassController {

    //Our collection of the class/wrapper objects cContact 
    public List<cContact> contactList {get; set;}

    //This method uses a simple SOQL query to return a List of Contacts
    public List<cContact> getContacts() {
        if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {
                 contactList.add(new cContact(c));
            }
        }
        return contactList;
    }


    public PageReference processSelected() {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<Contact> selectedContacts = new List<Contact>();

 
        for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }
     System.debug('These are the selected Contacts...');
        for(Contact con: selectedContacts) {
            system.debug(con);
        }
        return null;
    }


    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
        public cContact(Contact c) {
            con = c;
            selected = false;
        }
    }

 

VF Page:

 

<apex:page controller="wrapperClassController">
     
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!c.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column value="{!c.con.Name}" />
                <apex:column value="{!c.con.Email}" />
                <apex:column value="{!c.con.Phone}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form> 
bob_buzzardbob_buzzard

Not unless you tell us what the radio button options are I'm afraid.

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Here radio button options should be the list of all the records (Contacts) returned by the query... Out of those list of records I need to choose one and process that particular selected record... 

 

Thanks

bob_buzzardbob_buzzard

I'm not sure I quite understand you.  The contacts are backing the pageblocktable - are you looking for a radio button per row? If so, I don't think you can do that as the selectradio component won't spread across table rows in that way.

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Yes.. I am looking for a radiobutton per row... Is there any alternative to achieve that?

 

Thanks.

bob_buzzardbob_buzzard

If it were me, I'd stick with the checkboxes and use javascript to uncheck all the other boxes when I detected one had been checked.

b-Forceb-Force

you can simply used html checkbox, which will work like radio button , you need to select one record and process it

 

your updated VF page code

<apex:page controller="wrapperClassController">
     
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
<input type="radio" name ="rdSelected" checked="{!c.selected}" id="{!c.c.Id}"/>
</apex:column> <!-- This is how we access the contact values within our cContact container/wrapper --> <apex:column value="{!c.con.Name}" /> <apex:column value="{!c.con.Email}" /> <apex:column value="{!c.con.Phone}" /> </apex:pageBlockTable> </apex:pageBlock> </apex:form>



This will work for you ,

 

thanks,

Bala

bob_buzzardbob_buzzard

This would allow every contact to be selected.  OP wants to ensure only a single row is selected.

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Hello b-Force,

 

Radio button is working as expected but I am not able to capture the selected value in the controller class... Can you please let me know on how to capture the seleccted record in coontroller class... here is my controller class  

 

 

public class wrapperClassController {

    //Our collection of the class/wrapper objects cContact 
    public List<cContact> contactList {get; set;}

    //This method uses a simple SOQL query to return a List of Contacts
    public List<cContact> getContacts() {
        if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                contactList.add(new cContact(c));
            }
        }
        return contactList;
    }


    public PageReference processSelected() {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<Contact> selectedContacts = new List<Contact>();

        //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
        for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }
        if(SelectedContacts.size()==1){
            system.debug('This is the zise------->>>'+SelectedContacts);
        }
            
        // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
        System.debug('These are the selecteddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd Contacts...'+SelectedContacts.size());
        for(Contact con: selectedContacts) {
            system.debug(con);
        }
        return null;
    }


    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
        public cContact(Contact c) {
            con = c;
            selected = false;
        }
    }

 

In debug log I see that the selectedcontacts.size() as 0..

 

Thanks.

kiranmutturukiranmutturu

for that radio button u have to write the javascript function like onselect for that radio button and set the value for the server side...

b-Forceb-Force

you can WRITE javascript on radio button.

 

Create an apex  hidden field , update this using javascript by id of selected contact record, you can get same in c.c.Id

 

Now access this hidden field in controller, mean Id of Contact

 

 

Thanks,

Bala

 

This was selected as the best answer
Devendra@SFDCDevendra@SFDC

 

 

Hi Bob,



I have a similar requirement, where i am listing the records using checkboxes. But i want to restrict user from selecting more than one checkbox. 

 

You have suggested in your comment about using javascript and handle this particular requirement instead of using Radio Button.

 

Can you please what javascript statement/code need to write to handle this requirement??

 

It would be helpful.

 

Thanks and Regards,

Devendra S