Creating an extract value function on a Spark NumericStepper control in Flex 4

by Peter deHaan on June 1, 2009

in NumericStepper (Spark), beta1

The following example shows how you can set a display format function and extract value function on a Spark NumericStepper control in Flex 4 by setting the displayFormatFunction and extractValueFunction properties.

Full code after the jump.

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″. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/06/01/creating-an-extract-value-function-on-a-spark-numericstepper-control-in-flex-4/ -->
<s:Application name="Spark_NumericStepper_extractValueFunction_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <fx:Script>
        <![CDATA[
            private function displayFmtFunc(value:Number):String {
                return value + "mm";
            }

            private function extractValueFunc(value:String):Number {
                return parseInt(value);
            }
        ]]>
    </fx:Script>

    <s:NumericStepper id="numericStepper"
            minimum="0"
            maximum="100"
            displayFormatFunction="displayFmtFunc"
            extractValueFunction="extractValueFunc"
            horizontalCenter="0"
            verticalCenter="0" />

</s: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/2009/06/01/creating-an-extract-value-function-on-a-spark-numericstepper-control-in-flex-4/ -->
<s:Application name="Spark_NumericStepper_extractValueFunction_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">

    <fx:Script>
        <![CDATA[
            import spark.components.NumericStepper;

            private var numericStepper:NumericStepper;

            private function init():void {
                numericStepper = new NumericStepper();
                numericStepper.minimum = 0;
                numericStepper.maximum = 100;
                numericStepper.displayFormatFunction = displayFmtFunc;
                numericStepper.extractValueFunction = extractValueFunc;
                numericStepper.horizontalCenter = 0;
                numericStepper.verticalCenter = 0;
                addElement(numericStepper);
            }

            private function displayFmtFunc(value:Number):String {
                return value + "mm";
            }

            private function extractValueFunc(value:String):Number {
                return parseInt(value);
            }
        ]]>
    </fx:Script>

</s: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.

{ 2 comments… read them below or add one }

1 Joel February 4, 2010 at 12:53 pm

First of all, I believe displayFormatFunction has become valueFormatFunction and extractValueFuntion has become valueParseFunction.

Anyway, is there a way to get a blank value in the stepper when using snapInterval and stepSize? Anytime I pass null or “” back from valueFormatFunction it just sets it to the minimum value. I would like to have an unset state with a blank value.

Thanks,
Joel

Reply

2 Peter deHaan February 4, 2010 at 3:59 pm

@Joel,

Thanks. Most of the earlier Flex 4 examples are out of date and need to be updated [eventually].

As for your NumericStepper question, I don’t know of any ways to do that off the top of my head. Feel free to file an enhancement request at http://bugs.adobe.com/flex/ and the Flex team will consider adding the functionality if it gets enough votes/community support.

Peter

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: