Monday, June 23, 2014

How to use apex:actionFunction in Visualforce Page?

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

<apex:actionFunction>:
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:
Attribute
Description
action
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.
focus
The ID of the component that is in focus after the AJAX request completes.
id
An identifier that allows the actionFunction 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.
name
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.
onbeforedomupdate
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.
oncomplete
The JavaScript invoked when the result of an AJAX update request completes on the client.
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 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.
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 Example:

<apex:page controller="exampleCon">
    <apex:form >
        <!-- Define the JavaScript function sayHello-->
        <apex:actionFunction name="sayHello" action="{!sayHello}" rerender="out" status="myStatus"/>
    </apex:form>
     <apex:outputPanel id="out">
    <apex:outputText value="Hello "/>
    <apex:actionStatus startText="requesting..." id="myStatus">
        <apex:facet name="stop"> {!username}</apex:facet>
    </apex:actionStatus>
    </apex:outputPanel>
             
    <!-- Call the sayHello JavaScript function using a script element-->
    <script>window.setTimeout(sayHello,5000)</script>
             
    <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:outputPanel>
    <apex:form >
  
    <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
        <apex:param name="firstParam" assignTo="{!state}" value="" />
    </apex:actionFunction>
    </apex:form>
</apex:page>

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






0 comments:

Post a Comment

 
| ,