[SalesForce] How does reRender work in VisualForce

I have two fields in my SalesForce page.

  1. Site Field (newReq.Site__c)
  2. Requisition (newReq.Requisition_Type__c)

Based on my Requisition type, I want my site field to be visible and hidden. Here are the conditions on when the site field should be seen and when not.

Condition 1: When the user selects Requisition Type: "XYZ" I want site field to be hidden.

Condition 2: When the user selects anything other than XYZ then I want to show the site field again.

My 1st question: Now I am confused here. As in, what to use when. My Requsition field should be declared as reRender (code 1) or as rendered (code 2).

Secondly, So when I use reRender in my VisualForce page, do I need to add any code in my Controller ?

This is the code which I am trying to use:

Code 1

<apex:inputField id="reqType" styleClass="reqLineType" html-class="requist" 
value="{!newReq.Requisition_Type__c}"/> 
<apex:actionSupport event="onchange" reRender="siteMaterial"/>

Code 2

<apex:outputPanel id="thePanelWrapper">
<apex:outputPanel rendered="{!bRenderThePanel}">
<apex:inputField value="{!newReq.Site__c}"  id="siteName" html-class="hideSite"/> 
</apex:outputPanel>
</apex:outputPanel>

Best Answer

As an alternative, you can avoid the action= attribute on the apex:actionSupport as well as the action method and code your controller property as follows:

public Boolean getBRenderThePanel() {return newReq.requisition_type__c != 'XYZ';}

Note that although the VF page refers to

<apex:outputPanel rendered="{!bRenderThePanel}">

this resolves itself to the method getBRenderThePanel at compile time

When action is not included on actionSupport, the page will simply refresh after any changed values are sent to the controller. Hence, since the value of newReq.requisition_type__c is sent to the controller by actionSupport, when the page refreshes, the request to getBRenderThePanel is made which has the current value of requisition_type__c so it can return the proper true/false value.

Related Topic