官术网_书友最值得收藏!

Passing action methods to components

A controller action method is usually invoked from the Visualforce page that it is providing the logic for. However, there are times when it is useful to be able to execute a page controller action method directly from a custom component contained within the page. One example is for styling reasons, in order to locate the command button that executes the action method inside the markup generated by the component.

In this recipe we will create a custom component that provides contact edit functionality, including command buttons to save or cancel the edit, and a Visualforce page to contain the component and supply the action methods that are executed when the buttons are clicked.

How to do it…

This recipe does not require any Apex controllers, so we can start with the custom component.

  1. Navigate to the Visualforce Components setup page by clicking on Your Name | Setup | Develop | Components.
  2. Click on the New button.
  3. Enter ContactEdit in the Label field.
  4. Accept the default ContactEdit that is automatically generated for the Name field.
  5. Paste the contents of the ContactEdit.component file from the code download into the Visualforce Markup area and click on the Save button.
  6. Next, create the Visualforce page by navigating to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.
  7. Click on the New button.
  8. Enter ContactEditActions in the Label field.
  9. Accept the default ContactEditActions that is automatically generated for the Name field.
  10. Paste the contents of the ContactEditActions.page file from the code download into the Visualforce Markup area and click on the Save button.
  11. Navigate to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.
  12. Locate the entry for the ContactEditActions page and click on the Security link.
  13. On the resulting page, select which profiles should have access and click on the Save button.

How it works…

Opening the following URL in your browser displays the ContactEditActions page: https://<instance>/apex/ContactEditActions?id=<contact_id>.

Here, <instance> is the Salesforce instance specific to your organization, for example, na6.salesforce.com, and <contact_id> is the ID of any contact in your Salesforce instance.

The Visualforce page simply includes the custom component, and passes the Save and Cancel methods from the standard controller as attributes.

<apex:page standardController="Contact">
  <apex:pageMessages />
  <apex:form >
    <c:ContactEdit contact="{!contact}" saveAction="{!save}"
    cancelAction="{!cancel}" />
  </apex:form>
</apex:page>

The ContactEdit custom component declares attributes for the action methods of type ApexPages.Action.

<apex:attribute name="SaveAction" 
    description="The save action method from the page controller"
    type="ApexPages.Action" required="true"/>
<apex:attribute name="CancelAction" 
  description="The cancel action method from the page controller" 
  type="ApexPages.Action" required="true"/>

These attributes can then be bound to the command buttons in the component in the same way as if they were supplied by the component's controller.

<apex:commandButton value="Save" action="{!SaveAction}" />
<apex:commandButton value="Cancel" action="{!CancelAction}" 
    immediate="true" />

There's more…

While this example has used action methods from a standard controller, any action method can be passed to a component using this mechanism, including methods from a custom controller or controller extension.

See also

  • The Updating attributes in component controllers recipe in this chapter shows how a custom component can update an attribute that is a property of the enclosing page controller.
主站蜘蛛池模板: 小金县| 水城县| 大同县| 武川县| 辛集市| 玛多县| 泸溪县| 勃利县| 马公市| 屯昌县| 乐清市| 阜新市| 泸州市| 苏尼特右旗| 娄烦县| 万盛区| 大洼县| 聂拉木县| 东兴市| 东兰县| 凌源市| 泗阳县| 开化县| 成武县| 呈贡县| 克东县| 柳江县| 英超| 长春市| 德兴市| 万安县| 明溪县| 出国| 钟山县| 称多县| 大荔县| 罗甸县| 灵川县| 长乐市| 依兰县| 旬邑县|