Detecting when the user changes the selected month in the DateChooser control in Flex

by Peter deHaan on April 29, 2008

in DateChooser

The following example shows how you can detect when the user changes the selected month in the Flex DateChooser control by listening for the scroll event.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/29/detecting-when-the-user-changes-the-selected-month-in-the-datechooser-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.DateChooserEvent;

            private function dateChooser_scroll(evt:DateChooserEvent):void {
                arrColl.addItem(evt);
            }
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="arrColl" />

    <mx:DateChooser id="dateChooser"
            scroll="dateChooser_scroll(event);" />

    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            itemRenderer="mx.controls.Label"
            width="100%"
            height="100%" />

</mx:Application>

View source is enabled in the following example.

And since this is being requested a bit more lately, here’s the “same” example, but in ActionScript instead of MXML:

comps/MyDateChooser.as

/** http://blog.flexexamples.com/2008/04/29/detecting-when-the-user-changes-the-selected-month-in-the-datechooser-control-in-flex/ */
package comps {
    import mx.collections.ArrayCollection;
    import mx.containers.VBox;
    import mx.controls.DataGrid;
    import mx.controls.DateChooser;
    import mx.controls.Label;
    import mx.core.ClassFactory;
    import mx.events.DateChooserEvent;

    public class MyDateChooser extends VBox {

        [Bindable]
        private var arrColl:ArrayCollection;

        private var dateChooser:DateChooser;
        private var dataGrid:DataGrid;

        public function MyDateChooser() {
            super();
            initApp();
        }

        public function initApp():void {
            setStyle("horizontalAlign", "center");

            arrColl = new ArrayCollection();

            dateChooser = new DateChooser();
            dateChooser.addEventListener(DateChooserEvent.SCROLL, dateChooser_scroll);
            addChild(dateChooser);

            dataGrid = new DataGrid();
            dataGrid.dataProvider = arrColl;
            dataGrid.itemRenderer = new ClassFactory(mx.controls.Label);
            dataGrid.percentWidth = 100;
            dataGrid.percentHeight = 100;
            addChild(dataGrid);
        }

        private function dateChooser_scroll(evt:DateChooserEvent):void {
            arrColl.addItem(evt);
        }
    }
}

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/29/detecting-when-the-user-changes-the-selected-month-in-the-datechooser-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        xmlns:comps="comps.*"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <comps:MyDateChooser width="100%" height="100%" />

</mx:Application>

View source is enabled in the following example.

{ 6 comments… read them below or add one }

1 Claudiu April 29, 2008 at 11:16 pm

Great job! I’m watching you and every tutorial is helping me to understand Flex. Thank you

Reply

2 Charles May 2, 2008 at 6:32 am

Thanks for this post. I spend hours to find the solution to do this.

Charles, http://www.tldsco.com

Reply

3 André Baptista April 26, 2009 at 10:25 am

Hello!

This is part of what I was looking for. I know can detect that an event occurred in the next or previous month button.

But how can I detect the Month and Year without selecting a specific day within the DateChooser control ? (I want to get the label info of the month and year I’m currently looking at…)

Thanks a lot !

Cheers.

Reply

4 Peter deHaan April 27, 2009 at 7:30 am

André Baptista,

Look into the displayedMonth and displayedYear properties. You can use data binding or listen for the DateChooserEvent.SCROLL event, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[
            import mx.events.DateChooserEvent;

            private function dateChooser_scroll(evt:DateChooserEvent):void {
                trace("displayeYear:", dateChChChooser.displayedYear);
                trace("displayedMonth:", dateChChChooser.displayedMonth);
                trace("displayedMonth as String:", dateChChChooser.monthNames[dateChChChooser.displayedMonth]);
                trace("");
            }
        ]]>
    </mx:Script>

    <mx:DateChooser id="dateChChChooser"
            yearNavigationEnabled="true"
            scroll="dateChooser_scroll(event);" />
    <mx:Label text="{dateChChChooser.displayedYear}" />
    <mx:Label text="{dateChChChooser.displayedMonth}" />
    <mx:Label text="{dateChChChooser.monthNames[dateChChChooser.displayedMonth]}" />

</mx:Application>

Peter

Reply

5 Emma Ka September 9, 2009 at 1:11 am

You’ve extraordinary helpful. I truly appreciate it. Thank you! I will be forever able to capture a specific displayedMonth and/or Year in any DateChooser and bind it to a related event, because you gave a damn.

Reply

6 Subash December 7, 2009 at 10:48 pm

Peter, thank you for displayedMonth and displayedYear properties stuff. I was exactly looking for it. Thanks..

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: