error77
asked on
Searching between 2 DateField dates
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).imageur l;
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
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).imageur
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
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 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()));
}
ASKER
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
Cannot get it to work, so I'm not understanding what I need to do.
Hope you can help
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
ASKER
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/loa dContent() [E:\dev\4. x\framewor ks\project s\framewor k\src\mx\c ontrols\SW FLoader.as :1898]
at mx.controls::SWFLoader/loa d()[E:\dev \4.x\frame works\proj ects\frame work\src\m x\controls \SWFLoader .as:1515]
at mx.controls::SWFLoader/com mitPropert ies()[E:\d ev\4.x\fra meworks\pr ojects\fra mework\src \mx\contro ls\SWFLoad er.as:1379 ]
at mx.core::UIComponent/valid atePropert ies()[E:\d ev\4.x\fra meworks\pr ojects\fra mework\src \mx\core\U IComponent .as:7933]
at mx.managers::LayoutManager /validateP roperties( )[E:\dev\4 .x\framewo rks\projec ts\framewo rk\src\mx\ managers\L ayoutManag er.as:572]
at mx.managers::LayoutManager /doPhasedI nstantiati on()[E:\de v\4.x\fram eworks\pro jects\fram ework\src\ mx\manager s\LayoutMa nager.as:7 30]
at mx.managers::LayoutManager /doPhasedI nstantiati onCallback ()[E:\dev\ 4.x\framew orks\proje cts\framew ork\src\mx \managers\ LayoutMana ger.as:107 2]
Any ideas why?
thanks
Error: Unable to load ''.
at mx.controls::SWFLoader/loa
at mx.controls::SWFLoader/loa
at mx.controls::SWFLoader/com
at mx.core::UIComponent/valid
at mx.managers::LayoutManager
at mx.managers::LayoutManager
at mx.managers::LayoutManager
Any ideas why?
thanks
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;
}
}
}
ASKER
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
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>
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.
private var picList:XMLList;
Just put that above the thingChange function definition, and then picList will no longer be an undefined property.
ASKER
Perfect! THanks very much :o)
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.