[SalesForce] retrieving the selected checkboxes

How can I retrieve the checkboxes that are selected (ticked or equal to true) on my VF page if these checkboxes were created dynamically?

The checkboxes are being displayed on my VF Page like this:

  <apex:selectCheckboxes value="{!choices}" onchange="CalcCheckBox(this)">  
    <apex:selectOptions value="{!listEvents}"/><br/>  
  </apex:selectCheckboxes>  

And these are my 2 methods used to generate the checkboxes:

//String array to keep track of the ids of choices  
public String[] choices { get; set; }

//listEvents of select options to populate select boxes  
public list<SelectOption> getlistEvents() {  
     list<SelectOption> options = new list<SelectOption>();  
       for (Presentation__c ow : [Select Id, Display_Name__c FROM Presentation__c WHERE (Status__c='Open' AND Date_of_Presentation__c >= TODAY) ORDER BY Date_of_Presentation__c ASC LIMIT 100]) {   
         options.add(new SelectOption(ow.Id, ow.miiEventV1__Display_Name__c));   
       }    
     return options;   
}  

Best Answer

The choices string array will contain just the selected values, as if it were a multi select pick list not using a field. If the array is empty, no values were selected.

Edit

Here's a working demo:

Controller

public class selectdyn {
    public String[] values { get; set; }
    public SelectOption[] valueList { get; set; }

    public selectdyn() {
        valueList = new SelectOption[0];
        values = new String[0];
        for(Account record: [select id, name from account limit 100])
            valuelist.add(new selectoption(record.id, record.name));
    }

    public string getOutputText() {
        return String.join(values, ',');
    }
}

Page

<apex:page controller="selectdyn">
    <apex:form id="form">
        <apex:selectCheckboxes value="{!values}">
            <apex:selectOptions value="{!valueList}"/>
        </apex:selectCheckboxes>
        {!outputText}
        <apex:commandButton value="List Selections" reRender="form"/>
    </apex:form>
</apex:page>
Related Topic