<apex:page standardController="Contact" extensions="DynamicComponentExample"> <apex:dynamicComponent componentValue="{!headerWithDueDateCheck}"/> <apex:form> <apex:inputField value="{!Contact.LastName}"/> <apex:commandButton value="Save" action="{!save}"/> </apex:form> </apex:page>
public class DynamicComponentExample { public DynamicComponentExample(ApexPages.StandardController con) { } public Component.Apex.SectionHeader getHeaderWithDueDateCheck() { date dueDate = date.newInstance(2011, 7, 4); boolean overdue = date.today().daysBetween(dueDate) < 0; Component.Apex.SectionHeader sectionHeader = new Component.Apex.SectionHeader(); if (overdue) { sectionHeader.title = 'This Form Was Due On ' + dueDate.format() + '!'; return sectionHeader; } else { sectionHeader.title = 'Form Submission'; return sectionHeader; } } }
Each dynamic component has access to a common set of methods and properties. You can review this list in the Apex Developer's Guide in the chapter titled “Component Class”.
Component.c.MyCustomComponent myDy = new Component.c.MyCustomComponent();
Component.MyCustomComponent myDy = new Component.MyCustomComponent();
Component.TheirName.UsefulComponent usefulC = new Component.TheirName.UsefulComponent();
Component.Apex.DataList dynDataList = new Component.Apex.DataList(id='myDataList', rendered=true);
Component.Apex.Detail detail = new Component.Apex.Detail(); detail.expressions.subject = '{!Account.ownerId}'; detail.relatedList = false; detail.title = false;
Component.Apex.OutputText head1 = new Component.Apex.OutputText(); head1.expressions.value = '{!IF(CONTAINS($User.FirstName, "John"), "Hello John", "Hey, you!")}';
Passing in values through expressions is valid only for attributes that support them. Using {! } outside of the expressions property will be interpreted literally, not as an expression.
Component.Apex.OutputText head1 = new Component.Apex.OutputText(); head1.escape = false; head1.value = '<h1>This header contains HTML</h1>';
Component.Apex.DataTable myTable = new Component.Apex.DataTable(var='item'); myTable.expressions.value = '{!items}'; Component.Apex.OutputText header = new Component.Apex.OutputText(value='This is My Header'); myTable.facets.header = header;
For more information on facets, see Best Practices for Using Component Facets.
You can add child nodes to a dynamic Visualforce component using the childComponents property. The childComponents property acts as a reference to a List of Component.Apex objects.
public Component.Apex.PageBlock getDynamicForm() { Component.Apex.PageBlock dynPageBlock = new Component.Apex.PageBlock(); // Create an input field for Account Name Component.Apex.InputField theNameField = new Component.Apex.InputField(); theNameField.expressions.value = '{!Account.Name}'; theNameField.id = 'theName'; Component.Apex.OutputLabel theNameLabel = new Component.Apex.OutputLabel(); theNameLabel.value = 'Rename Account?'; theNameLabel.for = 'theName'; // Create an input field for Account Number Component.Apex.InputField theAccountNumberField = new Component.Apex.InputField(); theAccountNumberField.expressions.value = '{!Account.AccountNumber}'; theAccountNumberField.id = 'theAccountNumber'; Component.Apex.OutputLabel theAccountNumberLabel = new Component.Apex.OutputLabel(); theAccountNumberLabel.value = 'Change Account #?'; theAccountNumberLabel.for = 'theAccountNumber'; // Create a button to submit the form Component.Apex.CommandButton saveButton = new Component.Apex.CommandButton(); saveButton.value = 'Save'; saveButton.expressions.action = '{!Save}'; // Assemble the form components dynPageBlock.childComponents.add(theNameLabel); dynPageBlock.childComponents.add(theNameField); dynPageBlock.childComponents.add(theAccountNumberLabel); dynPageBlock.childComponents.add(theAccountNumberField); dynPageBlock.childComponents.add(saveButton); return dynPageBlock; }
<apex:form> <apex:dynamicComponent componentValue="{!dynamicForm}"/> </apex:form>
<apex:form> <apex:pageBlock> <apex:outputLabel for="theName"/> <apex:inputField value="{!Account.Name}" id="theName"/> <apex:outputLabel for="theAccountNumber"/> <apex:inputField value="{!Account.AccountNumber}" id="theAccountNumber"/> <apex:commandButton value="Save" action="{!save}"/> </apex:pageBlock> </apex:form>