3 Replies Latest reply on Mar 1, 2007 7:13 PM by doug777

    DateField Problem

    doug777 Level 1
      When I add a date formatter to the DateField control, selectedDate does not get set. It remains at null after using the date chooser.

      Does anyone know a way I can set selectedDate or is there a problem with the way I've used the DateFormatter?

      The following code shows the error. (The error disappears if you remove the labelFunction line.)

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

      <mx:Script>
      <![CDATA[
      import mx.controls.DateField;
      import mx.formatters.DateFormatter;

      public function initApp():void{
      var myDF:DateField = new DateField();
      myDF.labelFunction = formatDate;
      pan.addChild(myDF);
      }

      private function formatDate(date:Date):String{
      var df:DateFormatter = new DateFormatter();
      df.formatString = "D MMM YYYY";
      return df.format(date);
      }
      ]]>
      </mx:Script>

      <mx:Panel id="pan" width="450" height="150" />
      </mx:Application>

      Doug
        • 1. Re: DateField Problem
          webjoseph
          AFAIK, you would need to "bind" the response to the label (or hidden one). Or, you will need to create an event-listener to catch a user event when a date is selected. Hope this helps.
          • 2. Re: DateField Problem
            doug777 Level 1
            The selectedDate does get set after the date is selected.

            But when the formatter runs it appears to reset the selectedDate to null. This seems to happen after the updateComplete event so there seems to be no way to intervene as the formatter itself does not generate any events.

            Anyone got any ideas?

            Doug
            • 3. Re: DateField Problem
              doug777 Level 1
              This solves the problem.

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

              <mx:Script>
              <![CDATA[
              import mx.controls.DateField;
              import mx.formatters.DateFormatter;
              import mx.events.DropdownEvent;

              private var dc:Date;

              public function initApp():void{
              var myDF:DateField = new DateField();

              myDF.labelFunction = formatDate;
              myDF.addEventListener(DropdownEvent.OPEN, calOpenHandler);
              myDF.addEventListener(DropdownEvent.CLOSE, calCloseHandler);
              pan.addChild(myDF);
              }

              private function calOpenHandler(event:DropdownEvent):void{
              event.currentTarget.selectedDate = dc;
              }

              private function calCloseHandler(event:DropdownEvent):void{
              if(dc == event.currentTarget.selectedDate){
              event.currentTarget.selectedDate = null;
              }else{
              dc = event.currentTarget.selectedDate;
              }
              }

              private function formatDate(date:Date):String{
              var df:DateFormatter = new DateFormatter();
              df.formatString = "D MMM YYYY";
              return df.format(date);
              }
              ]]>
              </mx:Script>

              <mx:Panel id="pan" width="450" height="150" />
              </mx:Application>

              Doug