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

Data-driven decimal places

Attributes passed to custom components from Visualforce pages can be used wherever the merge syntax is legal. The <apex:outputText /> standard component can be used to format numeric and date values, but the formatting is limited to literal values rather than merge fields. In this scenario, an attribute indicating the number of decimal places to display for a numeric value cannot be used directly in the <apex:outputText /> component.

In this recipe we will create a custom component that accepts attributes for a numeric value and the number of decimal places to display for the value. The decimal places attribute determines which optional component is rendered to ensure that the correct number of decimal places is displayed, and the component will also bracket negative values. A Visualforce page will also be created to demonstrate how the component can be used.

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 DecimalPlaces in the Label field.
  4. Accept the default DecimalPlaces that is automatically generated for the Name field.
  5. Paste the contents of the DecimalPlaces.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 DecimalPlacesDemo in the Label field.
  9. Accept the default DecimalPlacesDemo that is automatically generated for the Name field.
  10. Paste the contents of the DecimalPlacesDemo.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 DecimalPlacesDemo 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 DecimalPlacesDemo page: https://<instance>/apex/DecimalPlacesDemo.

Here, <instance> is the Salesforce instance specific to your organization, for example, na6.salesforce.com.

The Visualforce page iterates a number of opportunity records and delegates to the component to output the opportunity amount, deriving the number of decimal places from the amount.

<c:DecimalPlaces dp="{!TEXT(MOD(opp.Amount/10000, 5))}"
  value="{!opp.Amount}" />

The component conditionally renders the appropriate output panel, which contains two conditionally rendered <apex:outputText /> components, one to display a positive value to the correct number of decimal places and another to display a bracketed negative value.

<apex:outputPanel rendered="{!dp=='1'}">  
  <apex:outputText rendered="{!AND(NOT(ISNULL(VALUE)), value>=0)}"
                value="{0, number, #,##0.0}">
    <apex:param value="{!value}"/>
  </apex:outputText>
  <apex:outputText rendered="{!AND(NOT(ISNULL(VALUE)), value<0)}"
                value="({0, number, #,##0.0})">
    <apex:param value="{!ABS(value)}"/>
  </apex:outputText>
</apex:outputPanel>

See also

  • The Data-driven styling recipe in Chapter 1, General Utilities shows how to conditionally color a numeric value based on whether it is positive or negative.
主站蜘蛛池模板: 汉寿县| 新野县| 耒阳市| 二连浩特市| 高唐县| 弥渡县| 平南县| 镇宁| 广丰县| 犍为县| 大洼县| 富阳市| 长岛县| 清徐县| 德州市| 宁蒗| 正蓝旗| 额尔古纳市| 定南县| 屏东市| 青阳县| 天长市| 清新县| 讷河市| 夏河县| 图们市| 衡阳市| 惠东县| 清河县| 和田县| 阿鲁科尔沁旗| 邻水| 大渡口区| 张北县| 日照市| 渝北区| 镶黄旗| 丽江市| 临汾市| 华安县| 海阳市|