You can pass parameters into your apex:actionFunction
directly, and access them through ApexPages.currentPage().getParameters().get('value')
, as illustrated below:
<apex:page controller="myController">
<apex:form >
<apex:actionFunction action="{!transform}" name="transformer" reRender="newvalue">
<apex:param name="source" value=""/>
</apex:actionFunction>
<apex:pageBlock >
<apex:pageBlockSection >
<input type="text" onchange="transformer(this.value)"/>
<apex:outputText value="{!newvalue}" id="newvalue"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public with sharing class myController {
public void transform() {
string source = apexpages.currentpage().getparameters().get('source');
// Do something with source here.
newvalue = source;
}
public String newvalue { get; set; }
}
This particular design has several benefits:
- You get full access to your view state, the method is not static.
- You keep parameters and unnecessary data out of your view state.
- You reduce your view state size by not requiring
apex:input*
or apex:select*
elements.
- A custom component is not required.
- You can serialize the data in JSON and parse it server-side for complex or non-string objects.
This is perhaps the least commonly known usage of apex:actionFunction
, and hasn't been mentioned up to this point, so I've included it here for completeness.
Updated: I just realized that you can't directly return a value through this function, so you'd still need a place to store the result. That being said, the resultant output could be stored to the view state directly, since you have full access to the view state.