Monday, June 23, 2014

How to use apex:actionFunction in Visualforce Page?

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

A component that 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.

Unlike < apex:actionSupport >, which only provides support for invoking controller action methods from other Visualforce components, < apex:actionFunction > defines a new JavaScript function which can then be called from within a block of JavaScript code.

This tag supports following attributes:
The action method invoked when the actionFunction is called by a JavaScript event elsewhere in the page markup. Use merge-field syntax to reference the method. For example, action="{!save}" references the save method in the controller. If an action is not specified, the page simply refreshes.
The ID of the component that is in focus after the AJAX request completes.
An identifier that allows the actionFunction component to be referenced by other components in the page.
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.
The name of the JavaScript function that, when invoked elsewhere in the page markup, causes the method specified by the action attribute to execute. When the action method completes, the components specified by the reRender attribute are refreshed.
The JavaScript invoked when the onbeforedomupdate event occurs--that is, when the AJAX request has been processed, but before the browser's DOM is updated.
The JavaScript invoked when the result of an AJAX update request completes on the client.
A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.
The ID of one or more components that are redrawn when the result of the action method 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.
The ID of an associated component that displays the status of an AJAX update request. See the actionStatus component.
The amount of time (in milliseconds) before an AJAX update request should time out.

Visualforce Example:

<apex:page controller="exampleCon">
    <apex:form >
        <!-- Define the JavaScript function sayHello-->
        <apex:actionFunction name="sayHello" action="{!sayHello}" rerender="out" status="myStatus"/>
     <apex:outputPanel id="out">
    <apex:outputText value="Hello "/>
    <apex:actionStatus startText="requesting..." id="myStatus">
        <apex:facet name="stop"> {!username}</apex:facet>
    <!-- Call the sayHello JavaScript function using a script element-->
    <p><apex:outputText value="Clicked? {!state}" id="showstate" /></p>
    <!-- Add the onclick event listener to a panel. When clicked, the panel triggers
    the methodOneInJavascript actionFunction with a param -->
    <apex:outputPanel onclick="methodOneInJavascript('Yes!')" styleClass="btn">
        Click Me
    <apex:form >
    <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
        <apex:param name="firstParam" assignTo="{!state}" value="" />

Controller class:

public class exampleCon {
    String uname;
   private String state = 'no';
    public String getUsername() {
        return uname;
    public PageReference sayHello() {
        uname = UserInfo.getName();
        return null;
    public void setState(String n) {
        state = n;
    public String getState() {
        return state;
    public PageReference methodOne() {
        return null;

save image


Post a Comment

| ,