How to use <apex:actionRegion> in Visualforce Page?
<apex:actionRegion>:
An area of a Visualforce page that demarcates which components should be processed by the Force.com server when an AJAX request is generated. Only the components in the body of the
< apex:actionRegion > are processed by the server, thereby increasing the performance of the page.
Note that an <apex:actionRegion> component only defines which components the server processes during a request—it does not define what area(s) of the page are re-rendered when the request completes. To control that behavior, use the rerender attribute on an <apex:actionSupport>,<apex:actionPoller>, < apex:commandButton >, < apex:commandLink >, < apex:tab >, or
<apex:tabPanel > component.
This tag supports following attributes:
Attribute
|
Description
|
id
|
An identifier that allows the component to be referenced by other components in the page.
|
immediate
|
A Boolean value that specifies whether the action associated with this component should happen immediately, without processing any validation rules associated with the fields on the page. If set to true, the action happens immediately and validation rules are skipped. If not specified, this value defaults to false.
|
rendered
|
A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.
|
renderRegionOnly
|
A Boolean value that specifies whether AJAX-invoked behavior outside of the actionRegion should be disabled when the actionRegion is processed. If set to true, no component outside the actionRegion is included in the AJAX response. If set to false, all components in the page are included in the response. If not specified, this value defaults to true.
|
Visualforce Example:
<apex:page standardController="Opportunity">
<apex:form >
<apex:pageBlock title="Edit Opportunity" id="thePageBlock" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<apex:inputField value="{!opportunity.name}"/>
<apex:pageBlockSectionItem >
<apex:outputLabel value="{!$ObjectType.opportunity.fields.stageName.label}" for="stage"/>
<apex:actionRegion >
<apex:inputField value="{!opportunity.stageName}" id="stage">
<apex:actionSupport event="onchange" rerender="thePageBlock" status="status"/>
</apex:inputField>
</apex:actionRegion>
</apex:pageBlockSectionItem>
<apex:inputfield value="{!opportunity.closedate}"/>
{!text(now())}
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
actionRegion visualforce salesforce
actionRegion provides an area of a Visualforce page that decides and separates which components should be processed by the force.com server when an AJAX request is generated. Only the components which are inside actionregion component are processed by server, so it increases visualforce page performance. Here components means, all visualforce tags like inputField, outputField, outputPanels etc.
actionregion is one of the most important tag which helps in increasing page performance. So we should try to make maximum use of actionregion in visualforce page. actionRegion component only defines which components the server processes during a request, it does not define which of the page are re-rendered when the request completes. We will still use reRender attribute on action component to decide area which should be rerendered AJAX request completes.
One more important point to note is that even when we use <apex:actionRegion> component, whole form is still submitted but only area which is inside actionRegion is processed by server.
I will explain importance of actionRegion with help of two examples. First example will not use actionRegion and second example will use actionRegion. Here in both example we are trying to show phone textbox, when we are selecting customer priority as high.
Example 1 Without <apex:actionRegion>
In this example, we are trying to show phone textbox when we are selecting high customer priority, then we are getting error as SLA and Account name is required. So validation fails.
Visualforce Page:
<apex:page controller="withoutActionregionController" tabStyle="Account">
<apex:form id="myform">
<apex:pageBlock id="pageId">
<apex:pageBlockSection title="If you will select High Customer Priority then phone textbox will be shown" columns="1" id="out" collapsible="false">
<apex:pageBlockSectionItem >
<apex:outputLabel value="{!$ObjectType.Account.fields.CustomerPriority__c.label}" for="priority"/>
<apex:inputField value="{!acc.CustomerPriority__c}" id="priority" >
<apex:actionSupport action="{!priorityChanged}" reRender="pageId" event="onchange"/>
</apex:inputField>
</apex:pageBlockSectionItem>
<apex:inputField value="{!acc.Phone}" rendered="{!showPhone}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Other Account Details" columns="2" collapsible="false">
<apex:inputField value="{!acc.SLA__c}" required="true"/>
<apex:inputField value="{!acc.Rating}"/>
<apex:inputField value="{!acc.name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller Class:
public class withoutActionregionController {
public Account acc{get;set;}
public Boolean showPhone{get;set;}
public withoutActionregionController(){
acc = new Account();
showPhone = false;
}
public PageReference priorityChanged(){
if(acc.CustomerPriority__c == 'High'){
showPhone = true;
}
else{
showPhone = false;
}
return null;
}
}
Now in second example we will use same code but with actionRegion tag. So in second example no validation error will be returned as only code inside actionregion component is processed by server.
Example 2 with <apex:actionRegion>
Visualforce Page:
<apex:page controller="withActionregionController" tabStyle="Account">
<apex:form id="myform">
<apex:pageBlock id="pageId">
<apex:pageBlockSection title="If you will select High Customer Priority then phone textbox will be shown" columns="1" id="out" collapsible="false">
<apex:pageBlockSectionItem >
<apex:outputLabel value="{!$ObjectType.Account.fields.CustomerPriority__c.label}" for="priority"/>
<apex:actionRegion >
<apex:inputField value="{!acc.CustomerPriority__c}" id="priority" >
<apex:actionSupport action="{!priorityChanged}" reRender="pageId" event="onchange"/>
</apex:inputField>
</apex:actionRegion>
</apex:pageBlockSectionItem>
<apex:inputField value="{!acc.Phone}" rendered="{!showPhone}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Other Account Details" columns="2" collapsible="false">
<apex:inputField value="{!acc.SLA__c}" required="true"/>
<apex:inputField value="{!acc.Rating}"/>
<apex:inputField value="{!acc.name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller :
public class withActionregionController {
public Account acc{get;set;}
public Boolean showPhone{get;set;}
public withActionregionController(){
acc = new Account();
showPhone = false;
}
public PageReference priorityChanged(){
if(acc.CustomerPriority__c == 'High'){
showPhone = true;
}
else{
showPhone = false;
}
return null;
}
}
Visualforce Action Region <apex:actionRegion> Example.
Hi,
In this post i am giving an example of <apex:actionRegion> usage in visualforce. it will use to achieve AJAX functionality to partial page refresh.
Controller Class:
public class ActionRegionCLS{
public String reqAccNumber{get;set;}
public Account accObj{get;set;}
public ActionRegionCLS(){
accObj = new Account();
}
public void fillRecord(){
if(reqAccNumber !=null && reqAccNumber !=''){
List<Account> accLst = new List<Account>();
accLst = [select id,Name,AccountNumber,Type,AnnualRevenue from Account where AccountNumber =:reqAccNumber];
if(accLst !=null && accLst.size()>0){
accObj = accLst[0];
}
else{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,' No Records found with this Account Number:'+reqAccNumber);
ApexPages.addMessage(myMsg);
accObj = null;
}
}
}
}
Visualforce Page :
<apex:page controller="ActionRegionCLS" >
<apex:pagemessages id="msgs"></apex:pagemessages>
<apex:form >
<apex:pageblock id="pb">
<table>
<apex:actionRegion >
<tr>
<td> <apex:outputLabel value="Enter Account Number"> </apex:outputLabel> </td>
<td>
<apex:inputtext value="{!reqAccNumber}"/>
<apex:commandButton value="Retrive" action="{!fillRecord}" rerender="pb,msgs"/>
</td>
</tr>
</apex:actionRegion>
<tr>
<td> <apex:outputLabel value="Account Name"> </apex:outputLabel> </td>
<td>
<apex:inputField value="{!accObj.Name}"/>
</td>
</tr>
<tr>
<td> <apex:outputLabel value="Account Type"> </apex:outputLabel> </td>
<td> <apex:inputField value="{!accObj.Type}"/> </td>
</tr>
</table>
</apex:pageblock>
</apex:form>
</apex:page>
Output :
once you enter account number value in Account Number text box and click on Retrive button it going to get the details of "Account Name", "Account Type" with out reloading entire page.using action region we are updating particular HTML area section with Rerender Tag.
No comments:
Post a Comment