function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Luke@PCALuke@PCA 

apex:selectManyCheckbox + apex:repeat?

Had a play with Visual Force as our app is all in clunky S-Controls at the moment and I'm very impressed with it so far, however have not found a way to get around the follow problem.

We want to have a way of selecting which countries apply using check boxes but the following code will not work:

<apex:selectManyCheckbox value="{!CountrySelection}" id="selectCheckbox" layout="pageDirection">
<apex:repeat value="{!Countries}" var="Country">
<apex:selectItem itemValue="{!Country}" itemLabel="{!Country}"/>
</apex:repeat>
</apex:selectManyCheckbox>

Custom Control Code:

public String[] getCountries() {
return new String[]{'GBR','USA','CAN','IRL'};
}

public List<String> getCountrySelection() {
List<String> countrylist = new List<String>();
countrylist.add('GBR');
countrylist.add('USA');
return countrylist;
}

Within the <apex:repeat> tags it is possible to output the country names with an <apex:outputText value="{!Country}/> and also possible to make an <apex:selectItem> using an API reference to a string outside of the repeat tags but cant seem to find a way to loop through countries and make an <apex:selectItem> for each.

Anyone else had problems with this?
mtbclimbermtbclimber
Luke, thanks for the post. Glad you are getting along well.

In response to your inquiry I have some good news and some bad news.

First the good news:

This is easier and more elegant than you were assuming.  What you are missing is the use of "selectItems" (note the s) component with the appropriate type in apex.  You can programatically build the value for selectItems with the selectOptions type in your controller. Here's the example:

Code:
PAGE:
<apex:page controller="sampleCon"> <apex:form> <apex:selectManyCheckbox value="{!countries}"> <apex:selectItems value="{!items}"/> </apex:selectManyCheckbox><br/> <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/> </apex:form> <apex:outputPanel id="out"> <apex:actionstatus id="status" startText="testing..."> <apex:facet name="stop"> <apex:outputPanel> <p>You have selected:</p> <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList> </apex:outputPanel> </apex:facet> </apex:actionstatus> </apex:outputPanel> </apex:page> CONTROLLER: public class sampleCon { String[] countries = new String[]{}; public PageReference test() { return null; } public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('US','US')); options.add(new SelectOption('CANADA','Canada')); options.add(new SelectOption('MEXICO','Mexico')); return options; } public String[] getCountries() { return countries; } public void setCountries(String[] countries) { this.countries = countries; } }

Now for the bad news.

As you might gather if you try the above code out the binding of the array is not working properly in the summer release.  We have fixed this in the Winter release and made some syntactic changes to make things more consistent and easier to understand. The following is the working version of this example from prerelease (no change to the controller, you would just use the page below with the controller from above):

Code:
<apex:page controller="sampleCon">
  <apex:form>
    <apex:selectCheckboxes value="{!countries}">
      <apex:selectOptions value="{!items}"/>
    </apex:selectCheckboxes><br/>
    <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
  </apex:form>
  <apex:outputPanel id="out">
    <apex:actionstatus id="status" startText="testing...">
      <apex:facet name="stop">
<apex:outputPanel> <p>You have selected:</p> <apex:dataList value="{!countries}" var="c">a:{!c}</apex:dataList>
</apex:outputPanel> </apex:facet> </apex:actionstatus> </apex:outputPanel> </apex:page>


Luke@PCALuke@PCA
Many thanks for the response Andrew!