Using Apex Param tag setting reference for the page elements
We all know that we can communicate with pages and controllers using Getter setter methods but sometime we will be stuck in the scenario like referencing the Visualforce parent components to the controller. Lets discuss an example we have a list of records say it’s a wrapper list and below are the data types I defined inside the wrapper
- String Name
- String Email
- Integer counter wrap
..
On clicking the delete link the entire row gets deleted but not the data. Let’s ask some questions to ourselves how to achieve this
1. How the compiler knows on which “Delete “ link is invoked inorder to get deleted?
2. How to pass the reference parameter to check which row the user intends to delete?
These questions will be answered by Apex Parameter tag
What does this apex param tag do??
Apex parameter tag always will be the child component for the following parent tags
<apex:commandlink>
<apex>commandbutton>
<Apex:actionstatus>
<apex:actionFunction>
<apex:actionSupport>
<apex:outputtext>
Let’s get back to our example
First things first before diving deep into any technical code let me give the answers for the questions I asked above
1. How the compiler know on which “Delete “ link of the row the list index position should get deleted ?
In the wrapper list I have set an Integer variable which will act as an index element for each record in a list and when the user click on the delete link the param tag will pass the value to the controller. There we can identify if the record is equal to the integer value obtained from the param tag. It its operation to delete or remove the row can be applied.
2. How to pass the reference parameter to check which row the user indents to delete?
I have set the value attribute from the integer value counter wrap
Scenario
Let’s look at some sample code here how we did this.
In the Visualforce page
<apex:pageBlockTable value="{!wrapkeyconlist2}" var="i"> <apex:column headerValue="Action"> <apex:commandLink value="Delete" action="{!remove}" immediate="true"> <apex:param name="index" value="{!i.counterWrap}"/> </apex:commandLink> </apex:column> <apex:column value="{!i.keyconlist1.name}"/> <apex:column value="{!i.keyconlist1.FF__Email__c}"/> </apex:pageBlockTable>
In the controller remove method
Integer param = Integer.valueOf(Apexpages.currentpage().getParameters().get('index')); for(Integer i=0;i<wrapkeyconlist2.size();i++){
if(wrapkeyconlist2[i].counterWrap == param ){ wrapkeyconlist2.remove(i);
}
counter--;
Nirmal Christopher
SFDC certified force.com developer
Global Tech and Resorces