passing parameters in action function

We can pass parameters to controller method that is being called using action function. For this we can use attribute "param" in visualforce.

Following is an example to demonstrate the parameter passing using param in action function. Clicking the checkbox calls the controller method using action function, first name and last name are passed to the controller using param attribute.


Visualforce Page

<apex:page controller="passparam">
 <apex:form>
  <apex:pageblock id="pgblck">
   First name : <apex:inputtext id="fn" value="{!firstname}">
   Last Name :<apex:inputtext id="ln" value="{!lastname}"> 

 Click this to See full name :  <apex:inputcheckbox onclick="calculate('{!$Component.fn}','{!$Component.ln}')">

Full Name:    <apex:outputtext value="{!fullname}"></apex:outputtext> 
  </apex:inputcheckbox></apex:inputtext></apex:inputtext></apex:pageblock>
<apex:actionfunction action="{!calpercentage}" name="calAF" rerender="pgblck">
<apex:param assignto="{!firstname}" name="param1" value="">
<apex:param assignto="{!lastname}" name="param" value="">
</apex:param></apex:param></apex:actionfunction>
 </apex:form>
 <script>
  function calculate(frst,lst){
  var Fname = document.getElementById(frst).value;
 var Lname = document.getElementById(lst).value;
   var res = confirm('Do you want to calculate?');
   if(res == true)
      calAF(Fname,Lname );
  }
 </script>
</apex:page>

Apex Class

Public class passparam{
Public string firstname{get;set;}
Public string lastname{get;set;}
Public string fullname{get;set;}
 Public void calpercentage(){
  fullname = firstname +' '+lastname;
 }

1 comment: