1 Reply Latest reply on Dec 11, 2009 9:06 AM by RK...

    how to use datagrid labelfunction to format currency

    madhooper Level 1

      So are I'm displaying item prices in a datagrid. I want to format the price to 2 decimal places and display a $. Here's the code i have so far. btw, it's not working. Please tell me what I'm missing.

       

      Here's my call to the labelfunc:

      <mx:DataGridColumn dataField="value" headerText="Price" labelFunction="price_labelFunc"/>

       

      Here's my formatter:

       

        <mx:CurrencyFormatter id="Price"
                  precision="2"
                  rounding="none"
                  decimalSeparatorTo="."
                  thousandsSeparatorTo=","
                  useThousandsSeparator="true"
                  useNegativeSign="true"
                  currencySymbol="$"
                  alignSymbol="left"/>

       

      Here's my labelfunc:

       

      public function price_labelFunc(item:Object, column:DataGridColumn):String
                {
                  return Price.format(item.value);
              }

      The label for the price is being returned in an arraycollection at "value".

      ie. value= 567.13445666666

       

       

      Thanks in advance for your assistance.

        • 1. Re: how to use datagrid labelfunction to format currency
          RK... Level 3

          I've used your code, which seems to be ok. Here is the sample,

           

          <mx:DataGrid id="dg" dataProvider="{_acItem}">
               <mx:columns>
                    <mx:Array>
                         <mx:DataGridColumn headerText="Item" dataField="item"  />
                         <mx:DataGridColumn headerText="Price" dataField="price" labelFunction="formatPrice" />
                    </mx:Array>
               </mx:columns>
          </mx:DataGrid>
          
          <mx:CurrencyFormatter id="cfPrice" 
                      precision="2"
                      rounding="none"
                      decimalSeparatorTo="."
                      thousandsSeparatorTo=","
                      useThousandsSeparator="true"
                      useNegativeSign="true"
                      currencySymbol="$"
                      alignSymbol="left"/>
          

           

          [Bindable]private var _acItem:ArrayCollection
                      
          public function init():void {
               var item: Object;
               _acItem = new ArrayCollection();
               item = {item:"PC", price:"1000.99"};
               _acItem.addItem(item);
               item = {item:"Laptop", price:"567.13545666666"};
               _acItem.addItem(item);
          }
          
          private function formatPrice(value: Object, column: DataGridColumn): String {
               return cfPrice.format(value.price);
          }
          

          1 person found this helpful