...

View Full Version : Resolved How to create datagrid from xml that changes depending on the day in flash builder 4


sparekey
07-17-2011, 01:18 AM
I have a datagrid in Flash Builder 4 that is successfully linked to an xml file. However, I need it to display different data from that same xml file depending of the day of week it is.

The xml is structured as follows.

<database>
<eatery>
<name>UT</name>
<open>
<sunday>1100</sunday>
<monday>1030</monday>
<tuesday>1030</tuesday>
<wednesday>1030</wednesday>
<thursday>1030</thursday>
<friday>1030</friday>
<saturday>1100</saturday>
</open>
<close>
<sunday>2000</sunday>
<monday>2100</monday>
<tuesday>2100</tuesday>
<wednesday>2100</wednesday>
<thursday>2100</thursday>
<friday>2100</friday>
<saturday>2100</saturday>
</close>
</eatery>
</database>


I want my datagrid to display the correct open and close times for only the current day.

I know how to find the date I just don't understand where I would go about writing an if else statement or such, or how to get the open and close data to change in my data grid.

I haven't been able to find an answer to this so if anyone could show me how to do this or point me somewhere that explains this I would greatly appreciate it!

Inigoesdr
07-17-2011, 07:39 PM
Something like this should work for you:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="500" minHeight="500"
creationComplete="application1_creationCompleteHandler(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<fx:XML id="db">
<database>
<eatery>
<name>UT</name>
<open>
<sunday>1100</sunday>
<monday>1030</monday>
<tuesday>1030</tuesday>
<wednesday>1030</wednesday>
<thursday>1030</thursday>
<friday>1030</friday>
<saturday>1100</saturday>
</open>
<close>
<sunday>2000</sunday>
<monday>2100</monday>
<tuesday>2100</tuesday>
<wednesday>2100</wednesday>
<thursday>2100</thursday>
<friday>2100</friday>
<saturday>2100</saturday>
</close>
</eatery>
</database>
</fx:XML>
</fx:Declarations>

<fx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
import mx.events.FlexEvent;

[Bindable]
private var day:String = '';

private var weekDayLabels:Array = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

protected function application1_creationCompleteHandler(event:FlexEvent):void
{
day = weekDayLabels[new Date().getDay()].toString().toLowerCase();
}

]]>
</fx:Script>

<s:DataGrid x="0" y="80" width="490" height="411" dataProvider="{new XMLListCollection(db.eatery)}">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="name" headerText="Name"></s:GridColumn>
<s:GridColumn dataField="open.{day}" headerText="Open"></s:GridColumn>
<s:GridColumn dataField="close.{day}" headerText="Close"></s:GridColumn>
</s:ArrayList>
</s:columns>
</s:DataGrid>
<s:Label x="10" y="10" width="199" height="26" text="Day of week: {day}"/>
</s:Application>

Compiled against Flex 4.5.1.

sparekey
07-18-2011, 07:34 PM
I guess the part that I really didn't know that was possible was using .{day} in the dataField. I'm new to actionscript and was completely stuck on this. Thanks for helping out!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum