6 Replies Latest reply on Mar 6, 2007 11:04 PM by Cheree Singal

    Resetting the datefield

    Cheree Singal
      Hi,

      On clicking a button, I want to reset the date chooser in a date field to show thw default date selection (i.e. cuurent date) and the box diplaying the selected date to be blank. I short, I want the date field to restore its default state. Any suggestions on how to acheive it?

      thanks in advance,
      Cheree
        • 1. Re: Resetting the datefield
          chris.huston.t10 Level 3
          There are at least two ways to do this.

          Short version:
          <mx:DateField x="82" y="168" id="myDate"/>
          <mx:Button x="229" y="168" label="reset" click="myDate.selectedDate = null"/>

          Extended version:

          <mx:Script>
          <![CDATA[
          private function reset_date():void {
          myDate.selectedDate = null;
          }
          ]]>
          </mx:Script>
          <mx:DateField x="82" y="168" id="myDate"/>
          <mx:Button x="229" y="168" label="reset" click="reset_date()"/>

          Vygo
          • 2. Re: Resetting the datefield
            Cheree Singal Level 1
            Thanks for replying.
            I have already tried this but it only makes the selected date blank. But on clicking the calender, it still opens the month and year of the previously selected date. I want that also to be reset.

            Cheree
            • 3. Resetting the datefield
              JohnLeger Level 1
              First of all do any of the dates before the current date need to be selectable, or can they be disabled? If they don't need to be selectable then the following will work...

              STEP ONE: Add a Script block with the following code

              <mx:Script>
              <![CDATA[
              /* Minuses one day off the DateField startRange BEGINS*/
              public static const millisecondsPerMinute:int = 1000 * 60;
              public static const millisecondsPerHour:int = 1000 * 60 * 60;
              public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;

              [Bindable] private var _startRange:Date = new Date(new Date().getTime()-(millisecondsPerDay));


              /* FOR A DATED _endRange ADD THE FOLLOWING CODE HERE AND BIND IT TO THE DateField...

              [Bindable] private var _endRange:Date = new Date(2008,11,31);

              FOR A NULL _endRange ADD THE FOLLOWING CODE HERE AND BIND IT TO THE DateField...

              [Bindable] private var _endRange:Date = null; */

              /* Minuses one day off the DateField startRange ENDS*/
              ]]>
              </mx:Script>


              STEP 2: Add your DateField

              <mx:DateField id="currentDateField"
              width="100"
              showToday="true"
              editable="true"
              formatString="MM/DD/YYYY"
              selectableRange="{{rangeStart: _startRange}}"/>


              STEP 3: Add your button as follows

              <mx:Button
              label="Reset"
              click="currentDateField.selectedDate=null; currentDateField.selectableRange={rangeStart: _startRange}"/>

              Let me know if this helps!

              John
              • 4. Re: Resetting the datefield
                chris.huston.t10 Level 3
                I see what you mean. It seems that you need to first open the dateField , make your changes, and then close it like this:

                <mx:Script>
                <![CDATA[
                private function reset_date():void {
                myDate.open();
                var curDate:Date = new Date();
                myDate.displayedMonth = curDate.getMonth();
                myDate.displayedYear = curDate.getFullYear();
                myDate.selectedDate = null;
                myDate.close();
                }

                ]]>
                </mx:Script>
                <mx:DateField x="82" y="168" id="myDate"/>
                <mx:Button x="229" y="168" label="reset" click="reset_date()"/>

                This code will clear the selected date and return the display to the current month/year. Let me know if this still doesn't get what you need.

                Vygo
                • 5. Re: Resetting the datefield
                  Cheree Singal Level 1
                  Thanks so much Vygo! That works just right!

                  Cheree
                  • 6. Re: Resetting the datefield
                    Cheree Singal Level 1
                    Thanks for replying John! The dates before current date had to be selectable.

                    Thanks,
                    Cheree