4 Replies Latest reply on Nov 26, 2006 8:11 PM by wy790289

    Format Numeric Field In Datafrid

    wy790289
      Hi,

      I would like to format a numeric column in datagrid to fix the number of decimal with comma, e.g. 9,999.9999. Could anyone please advise which keyword or how to do it.

      Thanks a lot!
      Wilson
        • 1. Re: Format Numeric Field In Datafrid
          peterent Level 2
          The easiest way is with a labelFunction on your column. For example, suppose your column is "value":

          <mx:DataGridColumn dataField="value" labelFunction="formatValue" ... />

          Then in a <mx:Script> block:

          private function formatValue( item:Object, col:* ) : String {
          var nfmt:NumberFormatter = new NumberFormatter();
          nfmt.precision = 4;
          return nfmt.format( item.value );
          }

          The second argument, col, is typed as * which means 'any type' - you could also declare it to be DataGridColumn but this is easier. The labelFunction, formatValue in this case, will be called for each visible row in that column and it will be passed a record from the DataGrid's dataProvider.

          For brevity's sake I included the creation of a NumberFormatter within the function. You should actually do this outside of the function and make the nfmt variable a member of the class. This way you won't be creating a new one each time this function is called.
          • 2. Re: Format Numeric Field In Datafrid
            Hanno1962
            Hi, I tried this code but my column remains blank after I add the function.

            Would I miss something
            • 3. Re: Format Numeric Field In Datafrid
              peterent Level 2
              I assume you substituted your dataField for "value" in my example?

              Use the debugger and set a break point in your labelFunction to make sure it is being called. Or, make the labelFunction return an explicit string like:

              return "works";

              to verify that "works" appears in every cell of that column.
              • 4. Re: Format Numeric Field In Datafrid
                wy790289 Level 1
                Thanks Peter,

                I can format the fields now.

                Hi Hanno, in above example the dataField "value" should be the name of the field you want to format. For example, if your field named "invoiceAmount" you should replace the string "item.value" to "item.invoiceAmount " in the return statement of function.

                However, I feel that the tutorial in adobe web is useful but it is difficult to found detail information. I ask this question about the formatting of numeric is because I cannot found any documents that has describe how to do it. May be Adobe should publish a reference manual of Flex...

                Wilson