Friday, March 23, 2012

visualforce dynamicComponent in apex salesforce

Hi all ,
This post is regarding the Visualforce Tag <apex:dynamicComponent> which is very useful feature in Visuaforce developement.
Hava a look on this.

This tag acts as a placeholder for your dynamic Apex components. It has one required parameter componentValue which accepts the name of an Apex method that returns a dynamic component.

Page:
====
<apex:page standardController="Contact" extensions="dynamicSample">
   <apex:dynamicComponent componentValue="{!headerWithDueDateCheck}"/>
   <apex:form >
        <apex:inputField value="{!Contact.LastName}"/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
  
     <apex:pageBlock title="This is dynamic Page Block Table">
     <!--<apex:pageBlockTable ></apex:pageBlockTable>-->
       <apex:dynamicComponent componentValue="{!PageBlockDate}"/>
     </apex:pageBlock>
 
</apex:page>

Class Code: ========
public class dynamicSample {

    public dynamicSample(ApexPages.StandardController controller) {
      
        public Component.Apex.PageBlockTable getPageBlockDate(){
            List<Contact> lstContact = [select id,Name,FirstName,LastName from Contact];
            Component.Apex.PageBlockTable pbTable = new Component.Apex.PageBlockTable();
            pbTable.value = lstContact ;
            pbTable.var = 'x';
            pbTable.columns =3;
            pbTable.border= '10' ;           
            Component.Apex.Column firstNameCol = new Component.Apex.Column();
            firstNameCol.expressions.value = '{!x.FirstName}';
            Component.Apex.Column firstNameCol1 = new Component.Apex.Column();
            firstNameCol1.expressions.value = '{!x.Id}';
            pbTable.childComponents.add(firstNameCol);
            pbTable.childComponents.add(firstNameCol1);
            return pbTable;
        }
  
        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;
        }
    }
}

3 comments:

  1. Excellent blog, good to see someone is posting quality information.

    Thanks for sharing this useful information.

    Keep up the good work

    Online IT Training & Corporate Trainings

    ReplyDelete
  2. I like the way you used the dynamic component, likewise im trying to use dynamic component but im struck at using the command link on the column. i have no clue on how to use the command link.. any ideas....

    thanks,
    Sandeep.

    ReplyDelete