【问题标题】:How to format a currency datafield in Flex如何在 Flex 中格式化货币数据字段
【发布时间】:2010-09-15 11:57:13
【问题描述】:

我有一个 xml 文件,为 Flex 2 中的数据网格提供数据,其中包括未格式化的价格字段(即:它只是一个数字)。 谁能告诉我如何获取该数据字段并对其进行格式化 - 添加货币符号,放入千位分隔符等。 谢谢。 S.

【问题讨论】:

    标签: apache-flex datagrid currency formatter


    【解决方案1】:

    非常感谢您的回答...他们帮助很大。

    最后我找到了一个涉及以下三个要素的解决方案:

    <mx:DataGridColumn headerText="Price" textAlign="right"  labelFunction="formatCcy" width="60"/>
    
    public function formatCcy(item:Object, column:DataGridColumn):String
            {
             return euroPrice.format(item.price);
            }
    
    <mx:CurrencyFormatter id="euroPrice" precision="0" 
        rounding="none"
        decimalSeparatorTo="."
        thousandsSeparatorTo=","
        useThousandsSeparator="true"
        useNegativeSign="true"
        currencySymbol="€"
        alignSymbol="left"/>
    

    我不知道这是否是正确的解决方案,但它似乎有效(目前), 再次感谢, S...

    【讨论】:

    • 这正是正确的解决方案,如果您所做的只是格式化单元格的内容,则不需要 itemRenderer。
    • 是的,西蒙。这是正确的解决方案。我完全同意你的看法
    【解决方案2】:

    如上所述,一种简单的方法是向指定列添加 labelFunction 并在其中格式化数据。

    我经常发现使用对象比直接使用 XML 更容易,所以通常如果我从一个函数接收 XML,我会为该 XML 创建一个对象和解析器,如果你喜欢,你也可以在解析器中格式化数据.

    另一种处理方法是在 itemRenderer 中。示例:

    <mx:DataGridColumn id="dgc" headerText="Money" editable="false">
        <mx:itemRenderer>
          <mx:Component>
             <mx:HBox horizontalAlign="right">
            <mx:CurrencyFormatter id="cFormat" precision="2" currencySymbol="$" useThousandsSeparator="true"/>
                <mx:Label id="lbl" text="{cFormat.format(data)}" />
             </mx:HBox>
          </mx:Component>
        </mx:itemRenderer>
    </mx:DataGridColumn>
    

    【讨论】:

    • 这太复杂了。 labelFunction 的要点是避免列中每个单元格的 itemRenderer 开销。在上面的示例中,每个单元格还有一个 CurrencyFormatter。下面的提问者解决方案是更好的选择。
    • 是的,我知道我只是提供了一个替代方案,因此为什么我在第一行声明要使用标签功能。
    【解决方案3】:

    CurrencyFormatter 类怎么样

    有关 Flex 2 的文档,请参阅 here。它非常易于使用。

    您可以在 DataGrid 列上的 labelFunction 中使用其中之一来格式化您的数字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多