Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to create datagrid from xml that changes depending on the day in flash builder 4

    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.
    Code:
    <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!
    Last edited by sparekey; 07-18-2011 at 07:30 PM.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Something like this should work for you:
    Code:
    <?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.

  • Users who have thanked Inigoesdr for this post:

    sparekey (07-18-2011)

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •