Monday, December 12, 2016

Visualforce - Beyond Basics Series 13 - Setting property using component

What are all the ways to set properties from Page ?

When the page loads, you can have a action method which will initialize the parameters in the Controller. So, that's the only way to set parameters, as most of the developers know.


What if you cannot edit Controller ?

In some cases, you can't edit Controller, because, there might be some policies in your company to create a new ticket, etc. Or simply you can't because, you need to write more Apex Tests, if you want to deploy changes in Controller.


Is there any other way to set parameters only by changing page ?

yes. When the page gets loaded, after initializing Page's Controller object & all Compenents Controller Objects, Salesforce initializes all Components. That means, we can  use that step to set parameters. It may confuse a bit, but, after looking the code, it will be very easy.

Where is the code ?

For illustration, i have controller with 2 property. Then, a component which will set the property with the value. Then, in the page, we use that component along with the attribute name to assign the value.


public class testcls_12122016 {
public String color {get; set;}
public Integer rank {get; set;}
}
view raw gistfile1.txt hosted with ❤ by GitHub
<apex:component >
<apex:attribute type="string" name="paramname" description="The name of the controller parameter"/>
<apex:attribute type="string" name="paramvalue" assignTo="{!paramname}" description="The value needs to be set"/>
</apex:component>
view raw gistfile1.txt hosted with ❤ by GitHub
<apex:page controller="testcls_12122016">
<c:setparameter paramname="{!color}" paramvalue="red"/>
<c:setparameter paramname="{!rank}" paramvalue="5"/>
<!-- Output Message -->
<apex:sectionHeader title="New Property Values"/>
<apex:outputText value="New Color Property value - {!color}"/>
<br/>
<apex:outputText value="New Rank Property value - {!rank}"/>
</apex:page>
view raw gistfile1.txt hosted with ❤ by GitHub


No comments:

Post a Comment

Thanks for reading my blog !