6

The "selected" attribute can be used to define if a checkbox should be checked by default when it is displayed on a Visualforce Page.

This works correctly when defined in a simple single apex:inputcheckbox tag in a form, but when this tag is present inside a repeat tag, the selected attribute has no affect. See the following example code:

<apex:form>    
  Single Checkbox:<br/>
  <apex:inputCheckbox selected="true"/>    

  <br/><br/>Repeated Checkbox:<br/>
  <apex:repeat value="{!availableProducts}" var="myVar">
      <apex:inputCheckbox selected="true"/>
  </apex:repeat>
</apex:form>

Here is the resulting output when I load the page:

single and multiple selected checkboxes

As you can see, the repeated checkbox doesn't get automatically selected. When inspecting the html, I see that the "checked" value doesn't get set in these repeated checkboxes like it does with the singular version. Am I missing something in terms of correct usage, or is this a problem with the attribute?

4
  • 4
    It seems that server can't bind direct boolean in repeat, but if you bind it to apex value - it will work. Jul 30, 2016 at 18:42
  • 1
    Yes I do see same issue with apex:inputcheckbox inside repeat but as an alternative solution you can bind boolean property of same object or use a wrapper class if require to bind value.
    – Himanshu
    Jul 30, 2016 at 20:22
  • 1
    Sounds like a candidate for known issue Jul 31, 2016 at 0:37
  • How did you get it to work? Can you help with my question please. It's similar to this one and I can't proceed without this fix. Thanks.
    – S..
    Feb 10, 2017 at 9:57

1 Answer 1

1

Whenever I've used an apex:inputCheckbox within an apex:repeat or apex:pageBlockTable I've always used the value attribute to indicate if the item is checked or not.

E.g.

Controller:

public class repeatCon {
    public Wrapper[] getBooleans() {
        return new Wrapper[]{new Wrapper(true), new Wrapper(false), new Wrapper(true), new Wrapper(false), new Wrapper(true) ,new Wrapper(false)};
    }

    public class Wrapper {
        public Boolean Value { get; set;}
        public Wrapper (Boolean bool) {
            this.Value = bool;
        }
    }
}

Visualforce:

<apex:page controller="repeatCon">
  <apex:form>    
      Single Checkbox:<br/>
      <apex:inputCheckbox selected="true"/>    

      <br/><br/>Repeated Checkbox:<br/>
      <apex:repeat value="{!booleans}" var="wrapper">
          <apex:inputCheckbox selected="true" value="{!wrapper.Value}"/>
      </apex:repeat>

      <p>Value Binding</p>
      <apex:repeat value="{!booleans}" var="wrapper">
          <apex:inputCheckbox value="{!wrapper.Value}"/>
      </apex:repeat>
    </apex:form>
</apex:page>

Output:

enter image description here

I suspect, and this is only a guess, that in the context of an iteration component the value attribute is overriding the selected attribute. Even if there is nothing configured for the value attribute.

1
  • Can you help with my question please. It's similar to this one and I can't proceed without this fix. Thanks.
    – S..
    Feb 10, 2017 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .