Most often we need to supply the arguments in <apex:actionFunction> and in this article I will demonstrate the way in which we can pass one or more than one parameter.
The output of the example will be like below screen.
Passing Parameter in ActionFunction in Visualforce
Create Apex class with following code:
1 | public with sharing class PassParameterActionFunction { |
2 | public String val{get;set;} |
3 | public String enteredText1{get;set;} |
4 | public String enteredText2{get;set;} |
8 | val = 'You have entered : 1 - '+enteredText1+' 2 -'+enteredText2; |
In above code, the variables “enteredText1“ and “enteredText2“ will be the parameters supplied by the javascript and variable “val“ will display the concatenated result.
Now create the Visualforce page with code:
1 | <apex:page controller="PassParameterActionFunction"> |
11 | <apex:outputPanel id="resultPanel"> |
12 | <apex:actionStatus startText="requesting..." stopText="" id="myStatus" /> |
14 | <b><apex:outputLabel value="{!val}" /></b> |
18 | <apex:inputText id="txt1" /> |
21 | <apex:inputText id="txt2" /> |
25 | <span class="pointer" onclick="callActionMethod()"> Click Me !!! </span> |
27 | <apex:actionFunction name="echo" action="{!echoVal}" reRender="resultPanel"status="myStatus"> |
28 | <apex:param name="firstParam" assignTo="{!enteredText1}" value="" /> |
29 | <apex:param name="secondParam" assignTo="{!enteredText2}" value="" /> |
32 | <script type="text/javascript"> |
33 | function callActionMethod() |
35 | var txtVal1 = document.getElementById("{!$Component.frm.txt1}").value; |
36 | var txtVal2 = document.getElementById("{!$Component.frm.txt2}").value; |
37 | /*Below Method is generated by "apex:actionFunction" which will call Apex Method "echoVal" */ |
38 | echo(txtVal1,txtVal2); |
The below code snippet is used to define the “actionFunction“ in visual force page.
To supply the parameter, we have to use “apex:param“ tag. Attribute “assignTo“ will assign the parameter to variable name specified in Apex code. Here we have assigned the value to variable “enteredText1“ and “enteredText2“.
1 | <apex:actionFunction name="echo" action="{!echoVal}" reRender="resultPanel"status="myStatus"> |
2 | <apex:param name="firstParam" assignTo="{!enteredText1}" value="" /> |
3 | <apex:param name="secondParam" assignTo="{!enteredText2}" value="" /> |
The resulting JavaScript function created by the visualforce will be “echo“ because we have set that name for the “apex:actionFunction“.
Attribute “action“ will call the method specified on Apex class and “status“ will show the Ajax request status.
Below JavaScript method is used to call the generated method by “apex:actionFunction“.
1 | function callActionMethod() |
3 | var txtVal1 = document.getElementById("{!$Component.frm.txt1}").value; |
4 | var txtVal2 = document.getElementById("{!$Component.frm.txt2}").value; |
As you can see that we have called the method “echo” with two arguments, because in “apex:actionFunction” we have specified the parameters for the method.
Method 2:
In this method, instead of creating two temporary variable in Apex page and assigning it using attribute “assignTo” of we can directly get the value in Apex code by something like using
1 | Apexpages.currentPage().getParameters().get('paramName'); |
So the resultant Apex code will be:
1 | public with sharing class PassParameterActionFunction { |
2 | public String val{get;set;} |
6 | val = 'You have entered : 1 - '+Apexpages.currentPage().getParameters().get('firstParam')+' 2 -'+Apexpages.currentPage().getParameters().get('secondParam'); |
Visualforce code:
1 | <apex:page controller="PassParameterActionFunction"> |
11 | <apex:outputPanel id="resultPanel"> |
12 | <apex:actionStatus startText="requesting..." stopText="" id="myStatus" /> |
14 | <b><apex:outputLabel value="{!val}" /></b> |
18 | <apex:inputText id="txt1" /> |
21 | <apex:inputText id="txt2" /> |
25 | <span class="pointer" onclick="callActionMethod()"> Click Me !!! </span> |
27 | <apex:actionFunction name="echo" action="{!echoVal}" reRender="resultPanel"status="myStatus"> |
28 | <apex:param name="firstParam" value="" /> |
29 | <apex:param name="secondParam" value="" /> |
32 | <script type="text/javascript"> |
33 | function callActionMethod() |
35 | var txtVal1 = document.getElementById("{!$Component.frm.txt1}").value; |
36 | var txtVal2 = document.getElementById("{!$Component.frm.txt2}").value; |
37 | /*Below Method is generated by "apex:actionFunction" which will call Apex Method "echoVal" */ |
38 | echo(txtVal1,txtVal2); |
No comments:
Post a Comment