[SalesForce] Include radio button for each row in PageblockTable

I am having a hard time to figure out how I can include the radio button select option within the PageBlockTable. I am searching all the cases related to a contact here.

Logic in my controller extension which gets all the cases where contact name match:

public List<Case> excase{get;set;}
public List<SelectOption> casesradio{get;set;}
public String radioValue{get;set;}
public Pagereference search() {
  excase = [Select Id, CaseNumber, Status, Subject from Case where contact.Name = : first + ' ' + last];
  for (Case cd: excase) {
   casesradio.add(new SelectOption(cd.Id, cd.Subject));
  }

  return null;
 }

I use PageblockTable tag to show it on the Page:

    <apex:pageblockTable value="{!excase}" var="exscase" id="caselist">
       <apex:column value="{!exscase.CaseNumber}"/>
       <apex:column value="{!exscase.Status}"/>
       <apex:column value="{!exscase.Subject}"/>
    </apex:pageblockTable>

I would also like user to select a particular case and update it.(My update has some logic which is irrelevant for this question). I use below code to show the cases as radio button selectOption:

    <apex:selectRadio label="Select Case to Update" value="{!radioValue}">
       <apex:selectOptions value="{!casesradio}"/>
    </apex:selectRadio>

This appears in a single line as shown in screenshot. My logic in extension for select option is(same as above):

public List<Case> excase{get;set;}
public List<SelectOption> casesradio{get;set;}
public String radioValue{get;set;}
public Pagereference search() {
  excase = [Select Id, CaseNumber, Status, Subject from Case where contact.Name = : first + ' ' + last];
  for (Case cd: excase) {
   casesradio.add(new SelectOption(cd.Id, cd.Subject));
  }

  return null;
 }

enter image description here

I am trying to get the radio button for each row in PagebLockTable as show in the screen shot. I would like to select the radio button and pass the selected case back to my extension when I click update. How can the code be modified to accomplish this.

Best Answer

Few days back I also have same problem. To resolve I use below steps:

  1. I created an inner class in which I store the name (Label of Radio button).

    class ContactName { String name; }

  2. Create a list of inner class and assign values to them.

    List<ContactName> cName = new List<ContactName>(); cName.add('This is a new case'); cName.add('This is old Case');

  3. on VF Page, use html radio button tag in outputPanel

<apex:pageBlockTable value="cName" var="cs"> <apex:outputPanel> <input type="radio" name="caseName"> <label>{!cs.name}</label> </apex:outputPanel> </apex:pageBlockTable>

It will helps you.

Related Topic