Toggling year navigation on a DateChooser control in Flex

The following example shows how you can toggle year-based navigation on a Flex DateChooser control by setting the yearNavigationEnabled property in MXML and ActionScript.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/24/toggling-year-navigation-on-a-datechooser-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="yearNavigationEnabled:">
                <mx:CheckBox id="checkBox" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:DateChooser id="dateChooser"
            yearNavigationEnabled="{checkBox.selected}" />

</mx:Application>

View source is enabled in the following example.

You can also set the yearNavigationEnabled property using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/24/toggling-year-navigation-on-a-datechooser-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function checkBox_change(evt:Event):void {
                dateChooser.yearNavigationEnabled = checkBox.selected;
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="yearNavigationEnabled:">
                <mx:CheckBox id="checkBox"
                        change="checkBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:DateChooser id="dateChooser" />

</mx:Application>

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/07/24/toggling-year-navigation-on-a-datechooser-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.CheckBox;
            import mx.controls.DateChooser;

            private var checkBox:CheckBox;
            private var dateChooser:DateChooser;

            private function init():void {
                checkBox = new CheckBox();
                checkBox.addEventListener(Event.CHANGE, checkBox_change);

                var formItem:FormItem = new FormItem();
                formItem.label = "yearNavigationEnabled:";
                formItem.addChild(checkBox);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                Application.application.addChildAt(appControlBar, 0);

                dateChooser = new DateChooser();
                addChild(dateChooser);
            }

            private function checkBox_change(evt:Event):void {
                dateChooser.yearNavigationEnabled = checkBox.selected;
            }
        ]]>
    </mx:Script>

</mx:Application>

3 thoughts on “Toggling year navigation on a DateChooser control in Flex

Comments are closed.