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

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.
主站蜘蛛池模板: 钟祥市| 高清| 惠东县| 辽宁省| 万年县| 如皋市| 黄龙县| 尼玛县| 孟津县| 庆元县| 葫芦岛市| 那曲县| 寿光市| 同江市| 电白县| 麻江县| 神池县| 五大连池市| 金山区| 杭州市| 新化县| 潍坊市| 唐河县| 梅州市| 舞阳县| 铅山县| 马鞍山市| 彭州市| 中江县| 西昌市| 孟连| 城固县| 荔浦县| 博白县| 扬中市| 泉州市| 浦江县| 朔州市| 天津市| 曲靖市| 赣州市|