Monday, December 12, 2016

Visualforce - Beyond Basics Series 17 - Pass actions to component

What types you can pass to component ?

Custom components can take these types,

  • Primitive Types
  • sObjects
  • Collections (Set, List, Map)
  • Classes in System Namespace
    •   SelectOption
    •   ApexPages.Action, etc
  • Custom Class/Interface

How to make component more generic ?

A component can take any form of data that could be either System defined type or custom type. So, that will cover most of the use case for components. But, as a bonus, you could pass ApexPages.Action type, which will be invoked during specific event inside the component.


How to do it ?

you need to add a attribute tag which will take the ApexPages.Action type & store it in a variable. Then, when some event happens in the component, it invokes the action.


Where is code ?

I have illustrated the simple action passed to a controller which will call this when user changes the value in the text field.



<apex:component >
<apex:attribute type="Account" name="record" description="The account record"/>
<apex:attribute type="ApexPages.Action" name="saveAction" description="Custom Save Action provided by client"/>
<apex:form >
<apex:outputLabel value="Enter you name here"/>
<apex:inputField value="{!record.Name}"/>
<apex:commandButton value="save" action="{!saveAction}"/>
</apex:form>
</apex:component>
view raw gistfile1.txt hosted with ❤ by GitHub
<apex:page standardController="Account">
<!-- This component take any Account record & pass a custom action to handle it.
Here, for simplicity, i have passed standard save action, but, you can pass any
action method defined in the controller/extension
-->
<c:testcmp_12122016 record="{!Account}" saveAction="{!save}"/>
</apex:page>
view raw gistfile1.txt hosted with ❤ by GitHub

No comments:

Post a Comment

Thanks for reading my blog !