Wednesday, 30 April 2014

Rendering a page block section based on conditions


set dynamically, Multiple Conditions for Rendered Attribute
set dynamically ,Rendering a page block section based on conditions

/*
   Consider one  custom object:-  A__c

and A__c has fields :-  First_Name__c, Last_Name__c, Age__c and Contact_Details__c

Suppose when user opted First then user can see 4 fields but When user opted Second then user can see only two fields.

*/


/*Code for Visualforce (Vf)  page*/

<apex:page standardController="A__c" extensions="renderedconditionalcntrl"  >

<apex:form >

<apex:pageBlock title="Rederer with condition">

        <apex:outputLabel value="My Options"> </apex:outputLabel>
         <apex:inputField label="My Option" value="{!Aobj.My_Option__c}">
             <apex:actionSupport event="onchange" action="{!changeevent}"  status="displaystatus" immediate="false" reRender="c1"/>            
        </apex:inputField>

<apex:pageBlockSection id="c1" columns="1" collapsible="false">

        <apex:pageBlockSectionitem rendered="{! IF( isFirstDiv || isSecondDiv,true,false)}" >
     
                <apex:outputLabel value="First Name"> </apex:outputLabel>
             <apex:inputField label="First Name" value="{!Aobj.First_Name__c}" />
        </apex:pageBlockSectionitem>
     
        <apex:pageBlockSectionitem rendered="{! IF( isFirstDiv || isSecondDiv,true,false)}" >
                <apex:outputLabel value="Last Name"></apex:outputLabel>
                          <apex:inputField label="Last Name" value="{!Aobj.Last_Name__c}" />
        </apex:pageBlockSectionitem>
     
     
          <apex:pageBlockSectionitem rendered="{!isFirstDiv}" >
                <apex:outputLabel value="Age"></apex:outputLabel>
                        <apex:inputField label="Age" value="{!Aobj.Age__c}"/>
        </apex:pageBlockSectionitem>
     
        <apex:pageBlockSectionitem rendered="{!isFirstDiv}">
                <apex:outputLabel value="Contact Number"></apex:outputLabel>
                          <apex:inputField label="Contact Number" value="{!Aobj.Contact_Details__c}"/>
        </apex:pageBlockSectionitem>

     

                 
 </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

/*Code for Apex class (extensions)*/


public with sharing class renderedconditionalcntrl {

    public boolean isFirstDiv{get;set;} //first flag
    public boolean isSecondDiv{get;set;} //second flag
    public A__c Aobj{get;set;}
    public renderedconditionalcntrl(ApexPages.StandardController controller) {
    
     Aobj = new A__c();
    }
    
    public void changeevent(){
    System.debug('~~~~~~~~~~~~~~~~~~~~~' + Aobj.My_Option__c );
    
    //Set your flag 
    
       isFirstDiv = false;     
       isSecondDiv  = false; 
       
        if(Aobj.My_Option__c  == 'First')
        isFirstDiv = true;
        
         else if(Aobj.My_Option__c  == 'Second'){            
            isSecondDiv  = true;             
        }
        else {
            isFirstDiv = false;
              isSecondDiv  = false;
        }
        
    }

}


Refer following Images for the above implementation :-