Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ItemRenderer using datefield

Posted on 2009-05-08
6
Medium Priority
?
5,241 Views
Last Modified: 2012-05-06
Hi,

I am trying to figure out a way to render dates that come from MySQL (YYYY-MM-DD).

This is the error message I get:

Type Coercion failed: cannot convert "2003-09-02" to Date.

I am using the following as a renderer:

dgc.itemRenderer = new ClassFactory(mx.controls.DateField);

Where dgc is an AdvancedDataGridColumn object.

I tried using a labelFunction to convert the string to a date using DateField.stringToDate, but it seems that the labelFunction is called after the renderer.
0
Comment
Question by:alon_sabi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:mplord
ID: 24348494
Trying to use a DateField as a an itemRenderer implies that you might want to edit the data using DateField as an itemEditor as well. If you just want to display the date and not edit it, you might as well forget the itemRenderer and have the date displayed as text.

If you do want the field to be editable using DateField as an itemEditor (as well as itemRenderer), any answer here is going to depend on whether you're setting the dataProvider of the AdvancedDataGrid using XML, or for example an ArrayCollection.

Could you clarify, and perhaps provide an example of how you're setting the dataProvider for the grid?
0
 

Author Comment

by:alon_sabi
ID: 24349817
Yes, I will need to edit the item as well. I am using httpservice to grab a jason encoded string from PHP.
I jason decode it into an array collection.

I am currently looping thtough the result set, and convert all of the date fields to date objects, and that seems to work ... But ... It is really not efficient. I do not want to loop through the items in PHP / flex ... Is there a way to do the convertion using a custom date renderer or just before the date rendere tries to use it ?
0
 

Author Comment

by:alon_sabi
ID: 24349949
BTW will it be more efficient to use XML (speed wise compared to json) ?
 
Also is there a better way than httpservice ?

I am completely new to Flex.
0
Take our survey for a chance to win!

As a valued customer of Targus, we’d like to ask you a few questions about us. As thanks, you will be automatically entered for a chance to win a $500 VISA gift card. To enter, just complete the survey by September 15, 2017.

 
LVL 10

Accepted Solution

by:
mplord earned 2000 total points
ID: 24350892
OK, using JSON decode, I assume your ArrayCollection is of 'Objects' which has properties for each value, so we can't subclass the Object to intercept getting/setting date on that object.

This solution overrides the AdvancedDataGridColumn which displays the date from a String value, and allows you to edit the date using DateField and this will update your objects with the correct string formatting.

Part 1:
I've mocked up a couple of objects which have a string date field for date of birth (dob)
Notice the new control ADGCDate which extends AdvancedDataGridColumn.

Part 2:
The code for ADGCDate (create in a file called ADGCDate alongside your main Application file).


Part 1:
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();" xmlns:mycontrols="*">
 
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
 
			[Bindable] private var model:ArrayCollection = new ArrayCollection();
			
			private function init():void
			{
				var person1:Object = new Object();
				person1['name'] = "Fred";
				person1['age'] = "35";
				person1['dob'] = "1974-02-23";
				var person2:Object = new Object();
				person2['name'] = "Jim";
				person2['age'] = "25";
				person2['dob'] = "1984-03-13";
				model.addItem(person1);
				model.addItem(person2);
				
				adg1.dataProvider = model;
			}
			
		]]>
	</mx:Script>
 
	<mx:AdvancedDataGrid id="adg1" designViewDataType="flat" width="377" height="186" horizontalCenter="0" verticalCenter="0" editable="true">
		<mx:columns>
			<mx:AdvancedDataGridColumn headerText="Name" dataField="name"/>
			<mx:AdvancedDataGridColumn headerText="Age" dataField="age"/>
			<mycontrols:ADGCDate headerText="DOB" dataField="dob"/>
		</mx:columns>
	</mx:AdvancedDataGrid>
	
	
</mx:Application>
 
 
Part 2:
 
<?xml version="1.0" encoding="utf-8"?>
<mx:AdvancedDataGridColumn
	xmlns:mx="http://www.adobe.com/2006/mxml"
	editable="true"
	rendererIsEditor="true"
	editorDataField="datevalue">
 
	<mx:itemRenderer>
		<mx:Component>
			<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" horizontalAlign="center">
			    <mx:Script>
			        <![CDATA[
			        
			        	public function get datevalue():String
			        	{
			        		return DateField.dateToString(df.selectedDate, "YYYY-MM-DD");
			        	}
			        
			            override public function set data(value:Object):void
			            {
			                if(value != null)
			                {
			                    super.data = value;
			                    df.selectedDate = DateField.stringToDate(value[outerDocument.dataField], "YYYY-MM-DD");
			                }
			            }
			           
			            private function onDataChange(e:Event):void
			            {
						 	var item:Object = super.data;
							item[outerDocument.dataField] = DateField.dateToString(df.selectedDate, "YYYY-MM-DD");
			            }
			        ]]>
			    </mx:Script>
			    <mx:DateField id="df" width="100%" enabled="{outerDocument.editable}" dataChange="onDataChange" formatString="YYYY-MM-DD"/>
			</mx:HBox>
		</mx:Component>
	</mx:itemRenderer>
</mx:AdvancedDataGridColumn>

Open in new window

0
 
LVL 10

Expert Comment

by:mplord
ID: 24350943
Oh, and as for speed comparisons between XML/JSON or anything else like AMFPHP etc. I think that's a seperate question entirely ;) - it depends on a number of circumstances.
0
 

Author Closing Comment

by:alon_sabi
ID: 31579654
Thank you, it worked ...
I did add a couple of small items to the ADGCDate file which I needed ...

I added a DateFormatter , and a labelFunction that uses the DateFormatter, so the PHP script can send a date format to show in the field (MMM-DD-YYYY for example for Sept-16-2009).

Thank you !
0

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
Article by: Shawn
IT teams define success as solving problems quickly. To enable ITSM modernization we have to think of adopting the tools and methods that will enable resolution of ITSM issues more quickly.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question