Visualforce pages that use dynamic bindings can reference the Apex Map and List data types in their markup.
public List<String> people { get { return new List<String>{'Winston', 'Julia', 'Brien'}; } set; } public List<Integer> iter { get { return new List<Integer>{0, 1, 2}; } set; }
<apex:repeat value="{!iter}" var="pos"> <apex:outputText value="{!people[pos]}" /><br/> </apex:repeat>
public Map<String,String> directors { get { return new Map<String, String> { 'Kieslowski' => 'Poland', 'del Toro' => 'Mexico', 'Gondry' => 'France' }; } set; }
<apex:repeat value="{!directors}" var="dirKey"> <apex:outputText value="{!dirKey}" /> -- <apex:outputText value="{!directors[dirKey]}" /><br/> </apex:repeat>
Use dynamic references to lists and maps in an <apex:inputText> tag to create forms using data that isn’t in your organization’s custom objects. Working with a single map can be much simpler than creating a series of instance variables in an Apex controller or creating a custom object just for the form data.
<apex:page controller="ListsMapsController"> <apex:outputPanel id="box" layout="block"> <apex:pageMessages/> <apex:form > <apex:repeat value="{!inputFields}" var="fieldKey"> <apex:outputText value="{!fieldKey}"/>: <apex:inputText value="{!inputFields[fieldKey]}"/><br/> </apex:repeat> <apex:commandButton action="{!submitFieldData}" value="Submit" id="button" rerender="box"/> </apex:form> </apex:outputPanel> </apex:page>
public class ListsMapsController { public Map<String, String> inputFields { get; set; } public ListsMapsController() { inputFields = new Map<String, String> { 'firstName' => 'Jonny', 'lastName' => 'Appleseed', 'age' => '42' }; } public PageReference submitFieldData() { doSomethingInterestingWithInput(); return null; } public void doSomethingInterestingWithInput() { inputFields.put('age', (Integer.valueOf(inputFields.get('age')) + 10).format()); } }
public with sharing class MapAccCont { Map<Integer, Account> mapToAccount = new Map<Integer, Account>(); public MapAccCont() { Integer i = 0; for (Account a : [SELECT Id, Name FROM Account LIMIT 10]) { mapToAccount.put(i, a); i++; } } public Map<Integer, Account> getMapToAccount() { return mapToAccount; } }
<apex:page controller="MapAccCont"> <apex:form> <apex:repeat value="{!mapToAccount}" var="accNum"> <apex:inputField value="{!mapToAccount[accNum].Name}" /> </apex:repeat> </apex:form> </apex:page>
public class ToolController { public Map<String, String> toolMap { get; set; } public String myKey { get; set; } public ToolController() { Map<String, String> toolsMap = new Map<String, String>(); toolsMap.put('Stapler', 'Keeps things organized'); } }
<apex:page controller="ToolController"> <!-- This renders an error on the page --> <apex:outputText value="{!toolMap['Paperclip']}" /> </apex:page>
<apex:page controller="ToolController"> <!-- This renders a blank space --> <apex:outputText value="{!toolMap[null]}" /> </apex:page>