2008-10-06 51 views
3

我有一個xml文件,用於爲Flex 2中的數據網格提供數據,其中包含未格式化的Price字段(即:它只是一個數字)。 任何人都可以告訴我如何採取該數據字段和格式 - 添加貨幣符號,投入千分離等 謝謝。 S.如何在Flex中格式化貨幣數據字段

回答

1

如上所述,一個簡單的方法是將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> 
+1

這是不必要的複雜。 labelFunction的要點是避免列中每個單元格的itemRenderer的開銷。在上面的例子中,每個單元格也有一個CurrencyFormatter。下面的提問者解決方案是更好的選擇。 – Simon 2008-10-07 08:00:17

+1

是的,我知道我只是給出一個替代方案,因此我說爲了使用標籤功能的第一行。 – JustLogic 2008-10-07 12:40:37

0

如何CurrencyFormatter類

的文檔從Flex的2.這是很容易使用見here

您可以在DataGrid列的labelFunction中使用其中的一個來格式化數字。

4

非常感謝您的回答......他們幫助了很多。

在我去,涉及以下三個元素的溶液結束:

<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 ...