Salesforce Dynamically Populated List of Checkboxes in Visualforce and Apex
Dynamically Populated List of Checkboxes in Visualforce and Apex
Below you will find details on how to create a dynamic checkbox group on a custom Salesforce page using Apex and Visualforce.
These examples will leverage Visualforce specific tags and Apex specific functionality to accomplish things that you would normally need an external library such as jQuery to acheive.
These examples will leverage Visualforce specific tags and Apex specific functionality to accomplish things that you would normally need an external library such as jQuery to acheive.
Dynamic Array of Checkboxes
Dynamically build an array of checkboxes and keep track of which boxes are checked. Checkboxes will be populated from a SOQL query from a custom object in Salesforce. Selected options will be tracked and persisted if you re-render the checkboxes.
Visualforce
Command button to refresh list of checkboxes
<apex:commandButton action="{!refresh}" reRender="customCheckboxPanel" status="loadingDetails">
This is just an example. A more practical use of this would be a onchange or onclick event such as if you had checkbox groups on multiple tabs.
Output panel that is refreshed to display dynamic list of checkboxes
<apex:outputpanel id="customCheckboxPanel" layout="block">
<apex:actionStatus id="loadingDetails"
startText="Loading..."
stopText=""/>
<br/>
<apex:selectCheckboxes value="{!choices}" layout="pageDirection">
<apex:selectOptions value="{!list}"/><br/>
</apex:selectCheckboxes>
</apex:outputpanel>
Apex
You will have an array of strings that will contain each id of each selected option.
You will also need a method that retrieves the options via a SOQL queries and loads them into selectionOption objects
Choice Array and Get Select Options Method
//String array to keep track of the ids of choices
public String[] choices { get; set; }
//List of select options to populate select boxes
public List<SelectOption> getList() {
List<SelectOption> options = new List<SelectOption>();
for (Your_Custom_Object__c ow : [
SELECT Name,id FROM Your_Custom_Object__c]) {
options.add(new SelectOption(ow.id, ow.Name));
}
return options;
}
The id is being set as the value and the Name is being set as the displayed label. Replace Your_Custom_Object__c with the name of the custom object you want to query.
You should now have a set of checkboxes. Checked options will automatically be tracked in the choices String array.
Checkbox Array Example
Dynamically populated checkboxes |
First of all, I would like to appreciate your efforts towards this piece of blog. This helps me a lot. Keep it up and keep sharing such posts.
ReplyDeleteA good communication skill is really important everyone should know. When it comes to good communication skills, a person should be good at speaking english. But many people face issues in speaking English fluently, for which they take online spoken english classes to improve their English speaking skills.
https://onlinetuitionclassesadda.blogspot.com/2023/01/benefits-of-spoken-english-classes.html
Benefits of Spoken English classes