[SalesForce] Sobject list as radio button Select Option

I am building a page where I have contact's first name and Last Name as an input field. When I enter the first and last name, and click search all the cases related to the contact should display as a list having a radio button alongside them.

I go the cases related to the contact for a List<Case> excase{get;set;} variable in my extension and able to show it using apex:pageblocktable but now the challenge is how to show this List as a radio button in my VF page:

Here is part of my VF Page where I am writing the logic:

<apex:pageblocksection title="Search Exsisting Cases" columns="1">
      <apex:input label="Contact First Name" value="{!first}" id="firstnameid"/>
      <apex:input label="Contact Last Name" value="{!last}" id="lastnameid"/>
       <apex:pageblockTable value="{!excase}" var="exscase" id="caselist">
            <!-- <apex:selectradio value="{!exscase.CaseNumber}"></apex:selectradio>-->
            <apex:column value="{!exscase.CaseNumber}"/>
            <apex:column value="{!exscase.Status}"/>
            <apex:column value="{!exscase.Subject}"/>
       </apex:pageblockTable>

The case should be displayed with a radio button to select
        <apex:selectRadio value="{!excase}">

    </apex:selectRadio>  

    </apex:pageblocksection>




   <apex:pageBlockButtons location="bottom">
     <apex:outputPanel id="buttons1">
        <apex:commandButton action="{!search}" value="Search" id="SearchButton"/>
        <apex:commandButton action="{!updatecase}"  value="Update" id="NewCase"/>
     </apex:outputPanel>
   </apex:pageBlockButtons>
 </apex:pageBlock>

Part of logic from my controller extension:

  public List<Case> excase{get;set;}

    public Pagereference search(){

    excase=[Select Id,CaseNumber,Status,Subject from Case where contact.Name=:first +' '+ last];

    return null;
    }

Best Answer

You want to create a method in your controller that generates a selectOption list for your cases that you then pass to your page from your controller. Something like:

public static list<selectOption> getMyOptnsLst(){
   list<selectOption>MyOptnsLst = new list<selectOption>();
   for (Case c:excase){
     MyList.add(new selectOption(c.Id,c.Subject));
   {
   return MyOptnsLst
}

Once you have the above, you'd then create another Pageblock Section within a form tag that contains something like below:

<apex:form>
   <apex:selectRadio value="{!subject}">
      <apex:selectOptions value="{!MyOptnsLst}"/>
   </apex:selectRadio><p/>
   <apex:commandButton value="Select" action="{!selectCase}" />
</apex:form>

Your controller would also need a method named selectCase and a public variable named subject to hold the value of the selected radio button.

You simply loop through your select list until you find the one that matches in your method.

Edit

To get the radio buttons to appear in separate columns, you'll need to apply some CSS along with something like this:

 <apex:outputLabel value="{!MyOptionsLst}" for="theSelectRadio">
 </apex:outputLabel>

You can also add a <BR> tag at the end of each line which may solve your issue.

Alternatively, you could use an apex repeat or put this all in a PageBlockTable. There are a variety of ways to do this. If you use a table, put the label in one column aligned right and the select option in another aligned left.