+ Start a Discussion
AQAQ 

Set checkbox values to true

I am displaying a list of items using the selectcheckboxes in a manner virtually identical to the example from the VisualForce documentation on page 210 as reflected below.

 

I am interested in checking some of the checkboxes as they are displayed, without user input.  What code would need to be added to this example in order for my checkboxes to appear checked as they are being displayed?

 

Thanks in advance for your help.

 

Jeff

 

<!-- Page: --> <apex:page controller="sampleCon"> <apex:form> <apex:selectCheckboxes value="{!countries}" title="Choose a country"> <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> /*** 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; } } Attributes

 

Best Answer chosen by Admin (Salesforce Developers) 
VisualForceVisualForce

Hi Jeff,

In ur constructor code, if u pass values for selectcheckboxes(countries according ur sample), u can get ur output....

public samplecon() { countries.add('US'); }

 

All Answers

VisualForceVisualForce

Hi Jeff,

In ur constructor code, if u pass values for selectcheckboxes(countries according ur sample), u can get ur output....

public samplecon() { countries.add('US'); }

 

This was selected as the best answer
AQAQ
Thanks, that worked like a charm.
45124512

Hi Friend,

 

Really I am very thankful for suggesting this solution.

Thanks again !

 

Regards,

Ajay :smileyvery-happy: