anusha(salesforce developer)

Monday 1 August 2016

How to use <apex:actionPoller>  in Visualforce Page?

<apex:actionPoller>:
A timer that sends an AJAX update request to the server according to a time interval that you specify. The update request can then result in a full or partial page update. You should avoid using this component with enhanced lists.

Note that if an < apex:actionPoller > is ever re-rendered as the result of another action, it resets itself. An < apex:actionPoller > must be within the region it acts upon. For example, to use an < apex:actionPoller > with an < apex:actionRegion >, the < apex:actionPoller > must be within the < apex:actionRegion >.


  1. interval: The time interval between AJAX update requests, in seconds. This value must be 5 seconds or greater, and if not specified, defaults to 60 seconds

  1. action: The action method invoked by the periodic AJAX update request from the component.

  1. reRender: Comma separated id’s of one or more components that are refreshed when request completes.
  This tag supports following attributes:
Attribute
Description
action
The action method invoked by the periodic AJAX update request from the component. Use merge-field syntax to reference the method. For example, action="{!incrementCounter}" references the incrementCounter() method in the controller. If an action is not specified, the page simply refreshes.
enabled
A Boolean value that specifies whether the poller is active. If not specified, this value defaults to true.
id
An identifier that allows the component to be referenced by other components in the page.
interval
The time interval between AJAX update requests, in seconds. This value must be 5 seconds or greater, and if not specified, defaults to 60 seconds. Note that the interval is only the amount of time between update requests. Once an update request is sent to the server, it enters a queue and can take additional time to process and display on the client.
oncomplete
The JavaScript invoked when the result of an AJAX update request completes on the client.
onsubmit
The JavaScript invoked before an AJAX update request has been sent to the server.
rendered
A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.
reRender
The ID of one or more components that are redrawn when the result of an AJAX update request returns to the client. This value can be a single ID, a comma-separated list of IDs, or a merge field expression for a list or collection of IDs.
status
The ID of an associated component that displays the status of an AJAX update request. See the actionStatus component.
timeout
The amount of time (in milliseconds) before an AJAX update request should time out.


Visualforce Page:

<apex:page controller="exampleCon2">
    <apex:form >     
        <apex:outputText value="Watch this counter: {!count}" id="counter"/>
        <apex:actionPoller action="{!incrementCounter}" rerender="counter" interval="15"/>     
    </apex:form>
</apex:page>

Controller Class:

public class exampleCon2 {  
Integer count = 0;
                         
    public PageReference incrementCounter() {
        count++;
        return null;
    }
                         
    public Integer getCount() {
        return count;
    }

}


save image




save image



What is actionpoller in visualforce?


Action poller acts as a timer in visualforce page. It is used to send an AJAX request to the server depending on the time interval (time interval has to be specified or else it defaults to 60 seconds).

In the action attribute a controller method gets called. The method gets called with a frequency defined by the interval attribute which has to be greater than 5 seconds.

In the following example action poller calls the method "CounterMethod" every 5 seconds where the variable "seconds" counter is updated. Rerender attribute refreshes the page block hence showing the updated value of variable "seconds".

Controller Class:
Public with sharing class actionpollerDemoController {
Public  Integer seconds{get;set;}
  Public actionpollerDemoController(){
   seconds = 0;
  }
  Public void CounterMethod(){
    seconds = seconds + 5;
  }
}

Visualforce Page:

<apex:page controller="actionpollerDemoController">
    <apex:form >
        <apex:pageBlock id="pgplck">
              <apex:actionPoller action="{!CounterMethod}" reRender="pgplck" interval="5"/>
                      {!seconds } seconds since the action poller was called !!
         </apex:pageBlock>
      </apex:form>
</apex:page>


Output :

save image


Time out can also be specified as an attribute in action poller. Once the time out point is reached it stops making AJAX callout to the server and controller method is no more called.

No comments:

Post a Comment