Using formatters

Macromedia Flex formatters are ActionScript components that you use to format data into strings. Formatters perform a one-way conversion of raw data to a formatted string. They are triggered just before data is displayed in a text field. Flex includes standard formatters that let you format currency, dates, numbers, phone numbers, and ZIP codes.

All Flex formatters are subclasses of the mx.formatters.Formatter class. The Formatter class declares a format() method that takes a value and returns a String.

For most formatters, when an error occurs, an empty string is returned and a description is saved to an error property. The error property is inherited from the Formatter superclass.

The following procedure describes the general process for using a formatter:

  1. Declare a formatter in your MXML code, specifying the appropriate formatting properties.
  2. Call the formatter's format() method within the curly braces ({ }) syntax for binding data, and specify the value to be formatted as an argument to the format() method.

The following example declares a DateFormatter with an MM/DD/YYYY date format, and binds the formatted version of a Date object returned by a web service to the text property of a TextInput control:

<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"> 
...
   <!-- Declare a formatter and specify formatting properties. -->
   <mx:DateFormatter id="StandardDateFormat" formatString="MM/DD/YYYY"/>

   <!-- Trigger the formatter while populating a string with data. -->
   <mx:TextInput text="Your order shipped on {StandardDateFormat.format(ws.result.date)}"/> 
</mx:Application>

Version 1.5

 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/15/flex_docs_en/00000729.htm