Formatting values in an FxNumericStepper control using a custom display format function in Flex Gumbo

by Peter deHaan on December 4, 2008

in FxNumericStepper, beta

The following example shows how you can format the value displayed in a Flex Gumbo FxNumericStepper control by setting a custom display format function using the displayFormatFunction property.

The displayFormatFunction property was added in build 4228. Download Adobe Flex Gumbo SDK.

Full code after the jump.

According to the [beta] documentation on the dataFormatFunction property:

Callback function that formats the value displayed in the text input field. The function takes a single Number as an argument and returns a formatted String.

The function has the following signature:

funcName(value:Number):String

The default value is undefined.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/04/formatting-values-in-an-fxnumericstepper-control-using-a-custom-display-format-function-in-flex-gumbo/ -->
<Application name="FxNumericStepper_displayFormatFunction_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Script>
        <![CDATA[
            private function numericStepper_dispFmtFunc(value:Number):String {
                return numFormatter.format(value);
            }
        ]]>
    </Script>

    <Declarations>
        <NumberFormatter id="numFormatter" precision="2" />
    </Declarations>

    <!-- The displayFormatFunction property was added in build 4228. -->
    <FxNumericStepper id="numericStepper"
            minimum="0"
            maximum="10"
            value="5"
            stepSize="0.01"
            valueInterval="0.01"
            displayFormatFunction="numericStepper_dispFmtFunc" />

</Application>

View source is enabled in the following example.

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/04/formatting-values-in-an-fxnumericstepper-control-using-a-custom-display-format-function-in-flex-gumbo/ -->
<Application name="FxNumericStepper_displayFormatFunction_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <Script>
        <![CDATA[
            import mx.components.FxNumericStepper;
            import mx.formatters.NumberFormatter;

            private var numFormatter:NumberFormatter;
            private var numericStepper:FxNumericStepper;

            private function init():void {
                numFormatter = new NumberFormatter();
                numFormatter.precision = 2;

                numericStepper = new FxNumericStepper();
                numericStepper.minimum = 0;
                numericStepper.maximum = 10;
                numericStepper.value = 5;
                numericStepper.stepSize = 0.01;
                numericStepper.valueInterval = 0.01;
                /* The displayFormatFunction property was added in build 4228. */
                numericStepper.displayFormatFunction = numericStepper_dispFmtFunc;
                addChild(numericStepper);
            }

            private function numericStepper_dispFmtFunc(value:Number):String {
                return numFormatter.format(value);
            }
        ]]>
    </Script>

</Application>

This entry is based on a beta version of the Flex Gumbo SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Gumbo SDK.

{ 1 comment… read it below or add one }

1 kelebashi December 5, 2008 at 1:21 am

Strong Flex!

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: