You need to sign in to do that
Don't have an account?
Saikishore 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.
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
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.
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,......
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:
Not unless you tell us what the radio button options are I'm afraid.
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
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.
Yes.. I am looking for a radiobutton per row... Is there any alternative to achieve that?
Thanks.
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.
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
This will work for you ,
thanks,
Bala
This would allow every contact to be selected. OP wants to ensure only a single row is selected.
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.
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...
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
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