Action tags in VisualForce Salesforce


Action tags-This tags support for calling action and refresh the field only not visualforce page.
  1. apex:actionFunction-
    Provides support for invoking controller action methods directly from JavaScript code using an AJAX request. An <apex:actionFunction> component must be a child of an <apex:form> component.
  1. apex:actionPoller
     A timer that sends an AJAX update request to the server according to a time interval that you specify. Update request can then result in a full or partial page update. You should avoid using this component with enhanced lists.
  1. apex:actionRegion
       This action tag is use AJAX request for particular field or component. Like we have change the value of pick list in visual force then request go to Server and come back through AJAX without Saving the VF page.
  1. apex:actionStatus
          A component that displays the status of an AJAX update request. An AJAX request can either be in progress or complete

  1. apex:actionSupport
     Adds AJAX support to another component, allowing the component to be refreshed asynchronously by server when a particular event occurs, such as a button click or mouse over
___________________________________________________________________________

Sample Example:


Apex Class would be this:

public class Controller{
    public List<SelectOption> Alphabets {getset;}
    public List<SelectOption> Fruits {getset;}
  
    public String SelectedAlphabet {getset;}
  
    /*A Constructor which will build the intial list of Alphabets*/
    public Controller(){
        Alphabets = new List<SelectOption>();
        Fruits    = new List<SelectOption>();
      
         /*This is to add the NONE option for our Picklists*/
        SelectOption option = new SelectOption('--None--', '--None--');
        Alphabets.add(option);
        Fruits.add(option);
      
        option = new SelectOption('A', 'A');
        Alphabets.add(option);
      
        option = new SelectOption('B', 'B');
        Alphabets.add(option);     
    }
   /*This Method that will actually build the Fruits list for us. The ActionFunction will be calling this function as and when a User changes an Alphabet from the 1st List.*/
    public void createFruitList(){
        /*Always clear the List when begin so that previous values will be removed.*/
        Fruits.clear();
     
        Fruits.add(new SelectOption('--None--', 'None'));
     
        if(SelectedAlphabet == 'A'){
            Fruits.add(new SelectOption('Apple','Apple'));
            Fruits.add(new SelectOption('Apricot','Apricot'));
        }
        else if(SelectedAlphabet == 'B'){
            Fruits.add(new SelectOption('Banana','Banana'));
            Fruits.add(new SelectOption('Blackberry','Blackberry'));
        }
    }
}

Visualforce Markup-
<apex:page controller="Controller">
    <apex:form>
        <apex:actionFunction action="{!createFruitList}" name="generateFruits" reRender="selFruits" />
        <br/>
        Select the Alphabet:
        <apex:selectList id="selAlphabets" value="{!SelectedAlphabet}" size="1" onchange="generateFruits()">
            <apex:selectOptions value="{!Alphabets}">
            </apex:selectOptions>
        </apex:selectList>
        <br/> 
        Select the Fruit:
        <apex:selectList id="selFruits" size="1">
            <apex:selectOptions value="{!Fruits}">
            </apex:selectOptions>
        </apex:selectList>           
    </apex:form>
</apex:page>

The Apex SelectList together with Apex SelectOptions are used forPicklists on Visualforce Pages. The value attribute in the Apex SelectList will carry or hold the currently selected value in the Picklist while the value attribute in the Apex Select Options will carry or hold the various Options(A, B or Apple, Banana etc). The size="1" makes sure that it is rendered as a Picklist and not aMulti-Select Picklist.

ActionFunction-: <apex:actionFunction action="{!createFruitList}" name="generateFruits" reRender="selFruits" />
The action is obviously the Server Side Apex Method that has to be executed. In our case it is thecreateFruitList. The name tag is nothing but a name by which we can refer this actionFunction from other places(in JavaScript) on the Visualforce page. The reRender will be assigned with the Id of that tag on the Visualforce page that has to be refreshed when the function has been finished executing. In our case, it will be nothing but, our second picklist. This is because, the createFruitList will generate the new list of fruits based on the selection from the 1st picklist and the new list will be displayed only when we refresh that small portion since you can notice that the 2nd picklist's Apex SelectOptions takes the {! Fruits } variable in ourApex Class as the source of data. And this is AJAX - we send a request to the Server and refreshed a small portion of the page without disturbing anything else on the page.

Now when do we call this ActionFunction ? Yes, it's obvious-when 1st picklist options are changed. And hence we set the onchange parameter in the first picklist as

<apex:selectList id="selAlphabets" value="{!SelectedAlphabet}" size="1" onchange="generateFruits()">

You can notice that we have called the Apex ActionFunction by its name - generateFruits. Well, that's the end of the story! The moral of the story - 'ActionFunctions are really awesome!'

Reference:
https://success.salesforce.com/answers?id=90630000000hU2kAAE

Sample Example:
http://www.saaspie.com/2014/12/09/action-components-in-salesforce-part1/
http://sfdcsrini.blogspot.com/2015/02/how-to-use-apexactionsupport-tag-in.html


Comments

Popular posts from this blog

All about workflow rule , limitation and important points with example

Custom Setting in Salesforce