Searching between 2 DateField dates

error77
error77 used Ask the Experts™
on
Hi all,
I'm using the following code the search for a specific date in the xml and it works fine
but what I need is something more tricky.

Image.source = pics.pic.(date == datechooser1.text).imageurl;

I've added 2 DateField's.
DateField1 is the from date and DateField2 is the to date.
So now I have 2 dates.
I have a node in the xml called date and looks link this: <date>10/05/2010</dates>
I need to modify the code or some sample code of how to do this please?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
If you write (date == datechooser1.text), it will translate to 0 or 1, depending whether it matches or not.

So you need to write a something else...

A for-loop or for-each loop that will go through the records, matching the date, and returning the relevant record.
Well I would suggest that you parse the date of your xml-node into a Date object using Date.parseDate(...).
You then have a startDate, endDate and curDate object. Now the check is easy:

if((startDate.getTime() <= curDate.getTime()) && (curDate.getTime() <= endDate.getTime())) {
    ... code that is executed if it matches.
}
you could put all nodes into an Array list and assign a filterFunction (http://cookbooks.adobe.com/post_Using_the_to_ArrayCollection_s_filterFunction-5441.html) ... in the filterFunction you simply integrate the above check:



collectionData.filterFunction = doFilter;

.
.
.

private function doFilter(item:Object):Boolean {
    var curDate:Date = Date.parse(item.date);
    return ((startDate.getTime() <= curDate.getTime()) && (curDate.getTime() <= endDate.getTime()));
}

Open in new window

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
Any change of a small sample that I can try?
Cannot get it to work, so I'm not understanding what I need to do.
Hope you can help
Thanks
Commented:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                width="100%" height="100%">
    <mx:Script><![CDATA[

        private var pics:XML =
                <pics>
                    <pic><date>10/05/2009</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\camera.png</imageurl> <title>Camera</title> </pic>
                    <pic><date>10/06/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\OkButton.png</imageurl> <title>OK</title> </pic>
                    <pic><date>10/07/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\ErrButton.png</imageurl> <title>ERR</title> </pic>
                    <pic><date>10/08/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\lock18x18.png</imageurl> <title>LOCK</title> </pic>
                    <pic><date>10/09/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\IBM_LOGO_2.png</imageurl> <title>IBM</title> </pic>
                    <pic><date>10/10/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\new-win-icon.gif</imageurl> <title>new</title> </pic>
                    <pic><date>10/15/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\Refresh.png</imageurl> <title>Refrexh</title> </pic>
                    <pic><date>10/18/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\warning.gif</imageurl> <title>Warn</title> </pic>
                    <pic><date>10/19/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\IBM_LOGO_2.png</imageurl> <title>IBM</title> </pic>
                    <pic><date>10/21/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\IBM_LOGO_2.png</imageurl> <title>IBM</title> </pic>
                </pics>;
        private var picList:XMLList;
        private function thingChange(event:Event):void {
            if(datefield1.selectedDate && datefield2.selectedDate){
            if(event.target != stepper){
                picList = pics.pic.(Date.parse(date) > datefield1.selectedDate).(Date.parse(date) < datefield2.selectedDate);
                stepper.maximum = picList.length() - 1;
            }
            Image.source = picList[stepper.value].imageurl;
            }
        }


        ]]></mx:Script>
       <mx:DateField id="datefield1" change="thingChange(event)"/>
           <mx:DateField id="datefield2" change="thingChange(event)"/>
       <mx:NumericStepper id="stepper" minimum="0" maximum="0" change="thingChange(event)"/>
    <mx:Image id="Image"/>
</mx:Application>

Open in new window

Commented:
Oops. You get a null object reference when you change the end date to limit the picList to fewer components than the stepper value. This should fix that:
        private function thingChange(event:Event):void {
            if(datefield1.selectedDate && datefield2.selectedDate){
            if(event.target != stepper){
                picList = pics.pic.(Date.parse(date) > datefield1.selectedDate).(Date.parse(date) < datefield2.selectedDate);
                var stepValue:int = stepper.value;
                if(stepValue > picList.length() - 1){
                    stepValue = picList.length() -1;
                }
                stepper.value = stepValue;
                stepper.maximum = picList.length() - 1;
            }
            Image.source = picList[stepper.value].imageurl;
            }
        }

Open in new window

Author

Commented:
I'm trying to test it but on opening the second datefield I get a popup with this error:

Error: Unable to load ''.
      at mx.controls::SWFLoader/loadContent()[E:\dev\4.x\frameworks\projects\framework\src\mx\controls\SWFLoader.as:1898]
      at mx.controls::SWFLoader/load()[E:\dev\4.x\frameworks\projects\framework\src\mx\controls\SWFLoader.as:1515]
      at mx.controls::SWFLoader/commitProperties()[E:\dev\4.x\frameworks\projects\framework\src\mx\controls\SWFLoader.as:1379]
      at mx.core::UIComponent/validateProperties()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7933]
      at mx.managers::LayoutManager/validateProperties()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:572]
      at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:730]
      at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1072]

Any ideas why?

thanks

Commented:
That will happen if you have a later start date than end date or if none of the dates in the xml are in range, so, let's add that bit of validation:
        private function thingChange(event:Event):void {
            if (datefield1.selectedDate && datefield2.selectedDate && datefield1.selectedDate <= datefield2.selectedDate) {
                if (event.target != stepper) {
                    picList = pics.pic.(Date.parse(date) >= datefield1.selectedDate).(Date.parse(date) <= datefield2.selectedDate);
                    if (stepper.value > picList.length() - 1) {
                        stepper.value = picList.length() - 1;
                    }
                    stepper.maximum = picList.length() - 1;
                }
                if (picList.length() > 0) {
                    Image.source = picList[stepper.value].imageurl;
                }
            }
        }

Open in new window

Author

Commented:
I get this error when trying to run it:

Description      Resource      Path      Location      Type
1120: Access of undefined property picList.      PieChartSample1.mxml      /test1/src      line 22      Flex Problem

Then if I continue and choose 19 of this month (on first datefield) and the 21 of this month (on the second datefield) I get that same popup with errors that I was getting before.

Sorry about this :o/

Attached is the code that I'm using.

thanks




<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
				width="100%" height="100%">
	<mx:Script><![CDATA[
		
		private var pics:XML =
			<pics>
				<pic><date>10/05/2009</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\camera.png</imageurl> <title>Camera</title> </pic>
				<pic><date>10/06/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\OkButton.png</imageurl> <title>OK</title> </pic>
				<pic><date>10/07/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\ErrButton.png</imageurl> <title>ERR</title> </pic>
				<pic><date>10/08/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\lock18x18.png</imageurl> <title>LOCK</title> </pic>
				<pic><date>10/09/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\IBM_LOGO_2.png</imageurl> <title>IBM</title> </pic>
				<pic><date>10/10/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\new-win-icon.gif</imageurl> <title>new</title> </pic>
				<pic><date>10/15/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\Refresh.png</imageurl> <title>Refrexh</title> </pic>
				<pic><date>10/18/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\warning.gif</imageurl> <title>Warn</title> </pic>
				<pic><date>10/19/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\IBM_LOGO_2.png</imageurl> <title>IBM</title> </pic>
				<pic><date>10/21/2010</date> <imageurl>C:\Development\TestFlex\src\testflex\assets\img\IBM_LOGO_2.png</imageurl> <title>IBM</title> </pic>
			</pics>;

		private function thingChange(event:Event):void {
			if (datefield1.selectedDate && datefield2.selectedDate && datefield1.selectedDate <= datefield2.selectedDate) {
				if (event.target != stepper) {
					picList = pics.pic.(Date.parse(date) >= datefield1.selectedDate).(Date.parse(date) <= datefield2.selectedDate);
					if (stepper.value > picList.length() - 1) {
						stepper.value = picList.length() - 1;
					}
					stepper.maximum = picList.length() - 1;
				}
				if (picList.length() > 0) {
					Image.source = picList[stepper.value].imageurl;
				}
			}
		}


		
		
	]]></mx:Script>
	<mx:DateField id="datefield1" change="thingChange(event)"/>
	<mx:DateField id="datefield2" change="thingChange(event)"/>
	<mx:NumericStepper id="stepper" minimum="0" maximum="0" change="thingChange(event)"/>
	<mx:Image id="Image"/>
</mx:Application>

Open in new window

Commented:
You accidentally left out line 18 from the first code sample I posted:


        private var picList:XMLList;

Just put that above the thingChange function definition, and then picList will no longer be an undefined property.

Author

Commented:
Perfect! THanks very much :o)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial