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

by Peter deHaan on June 1, 2009

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.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?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:

<?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 4 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 4 SDK.

{ 2 comments… read them below or add one }

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

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; .

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: