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

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.
主站蜘蛛池模板: 大洼县| 乐昌市| 新绛县| 高唐县| 洞头县| 静安区| 呼图壁县| 师宗县| 常熟市| 台北县| 新邵县| 柏乡县| 梓潼县| 沁源县| 六枝特区| 大化| 绍兴市| 闻喜县| 大庆市| 桃江县| 获嘉县| 嘉黎县| 玉田县| 峡江县| 和平县| 元谋县| 汉源县| 什邡市| 舟山市| 达拉特旗| 望奎县| 邢台县| 鄂托克前旗| 长沙县| 东明县| 庆元县| 贵南县| 上林县| 拜泉县| 七台河市| 汉源县|