0

In my below code in the section where I used radio button :

<apex:page standardController="Account" recordSetVar="accounts">
    <apex:form >



<apex:pageBlock title="Account List" id="account_list">
<table>
<tr><td>
<apex:repeat value="{!accounts}" var="account">
<apex:selectRadio>
<apex:selectOption itemValue="{!account.Id}" itemLabel="{!account.Name}" />

</apex:selectRadio>
</apex:repeat>
</td>
</tr>
</table>

<h1> Contacts</h1>

<table>
  <apex:repeat value="{!accounts }" var="acc">
    <tr> 
      <apex:repeat value="{!acc.Contacts}" var="cont">
      <td><apex:outputText value="{!cont.Name}"/></td>
      </apex:repeat>
    </tr>
  </apex:repeat>
</table>


</apex:pageBlock>

    </apex:form>
</apex:page>

Given that this is a radio button and only one radio button should be selected at a given time , however with my implementation of radio button I am able to select all the radio buttons at the same time. Any fix for this?

0

What you want here is one <apex:selectRadio> element with multiple <apex:selectOption/> elements grouped inside. Instead, you have multiple <apex:selectRadio> elements each with a single <apex:selectOption/> element, which is basically creating multiple groups of one radio button.

If you change your code to:

<apex:selectRadio>
  <apex:repeat value="{!accounts}" var="account">
    <apex:selectOption itemValue="{!account.Id}" itemLabel="{!account.Name}" />
  </apex:repeat>
</apex:selectRadio>

it should solve the problem.

UPDATE:

I just saw your other question about this. If the above doesn't work, you may need to create a SelectOption list using a controller extension, something like this:

public List<SelectOption> getItems() {
  List<SelectOption> options = new List<SelectOption>(); 
  for (Account a : myAccountsList) {
    options.add(new SelectOption(a.Id, a.Name))
  }

  return options; 
}

Then you add the options to your <apex:selectRadio> tag thusly:

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

See also the documentation on apex:selectRadio.

0

In this fragment of VF code

<apex:repeat value="{!accounts}" var="account">
  <apex:selectRadio>
   <apex:selectOption itemValue="{!account.Id}" itemLabel="{!account.Name}" />
  </apex:selectRadio>
</apex:repeat>

You are iterating over a collection {!accounts} that represents the records available to the standardsetController and for each account you are displaying a component. Hence, you have many selectRadio and each is independent of each other and hence selectable independently.

There are various workarounds to this such as by using checkboxes styled like radio buttons and jQuery or controller code to enforce that only one can be selected

  • Thanks, any resources I can refer to get me started on this? – SfdcBat Feb 9 '16 at 19:08
  • @shannonsans has one solution although it may have UI limitations for you if you have multiple columns to show; or - see forcetree.com/2011/09/… – cropredy Feb 9 '16 at 19:12
  • Yes - my solution will group them correctly but unfortunately renders them in an HTML table, because that's how apex:selectRadio works. – shannonsans Feb 9 '16 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.