bpurcell.org - Binding Complex XML to a DataGrid in Flex
Calendar
SunMonTueWedThuFriSat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

Subject Archives
Amazon EC2 (15)
ColdFusionMX (155)
Corvette (3)
Flash Lite (4)
Flash Media Server (5)
Flash Player (3)
Flex (39)
General Web Dev (14)
HDTV (3)
Jboss (1)
Jquery (2)
JRun (59)
Max 2003 (3)
Other (33)
PC Hardware (16)
Software (17)
SpatialKey (7)
Wireless (8)
Working Out (1)

RSS Feed
Feed Listing

Site Contents
Home
My Blog
ColdFusion
JRun
Flex
Wireless & Networking
Hardware & Gadgets
HDTV
Software Picks
Pictures
Contact Me
Search


My Articles & Papers
Flex Performance
Server/Client
mm.com Under the Hood
Multiple Instances of CFMX
Multiple Instance Config
NLB with ColdFusion
Clustering CFMX for J2EE
Multi-Tier Hardware LB w CFMX
Cisco CSS & Coldfusion MX
JRun 4 Jini based Clustering
WiFi Growth

2ID Tacweb

Other Hobbys
Body-For-Life Challenge

Personal Projects
Family Pool
Deck Gate

username:
password:
 

 
Viewing Individual Entry / Main
April 28, 2004

The datagrid in Flex works very well with flat XML. For example if you have the following XML it is very simple to bind it to a DataGrid.

<mx:Model id="thePersonModel">
 <name>Brandon</name>
 <age>30</age>
 <address>19 foobar</address>
</mx:Model>
<mx:DataGrid id="myDataGrid" dataProvider="{mx.utils.ArrayUtil.toArray(thePersonModel)}"/>

But what if you have a more complex XML packet that you need to bind to a DataGrid?

<mx:Model id="thePersonModel">
 <name>Brandon</name>
 <age>30</age>
 <address>
  <street>foobar</street>
  <number>19</number>
 </address>
</mx:Model>

<mx:DataGrid id="myDataGrid" dataProvider="{mx.utils.ArrayUtil.toArray(thePersonModel)}"/>

In the above code the name and age will bind appropriately but the addess column will show [object Object] instead of the value of street and number. There is an easy workaround to display the values of complex XML using the labelFunction from the <mx:DataGridColumn> tag. When we call the labelFunction tag the dataprovider gets passed to the function and we can grab any value from the dataprovider. The following code shows how this works

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" 
width="600" height="450">
<mx:Script> <![CDATA[ function myLabelFunc(item):String { if (item.address.number==undefined) {return null;} else {return item.address.number + " " + item.address.street;} } ]]> </mx:Script> <mx:Model id="thePersonModel"> <name>Brandon</name> <age>30</age> <address> <street>foobar</street> <number>19</number> </address> </mx:Model>
<mx:DataGrid id="myDataGrid2"
dataProvider="{mx.utils.ArrayUtil.toArray(thePersonModel)}">
<mx:columns> <mx:Array> <mx:DataGridColumn columnName="name" headerText="Name"/> <mx:DataGridColumn columnName="age" headerText="Age"/> <mx:DataGridColumn headerText="Address" labelFunction="myLabelFunc"/> </mx:Array> </mx:columns> </mx:DataGrid> </mx:Application>

While this is a very simple example where we have XML data hard coded to a Model there may be times where you get data from web services that you have no control over the format of the data. labelFunction can provide an easy workaround to get the appropriate data into your DataGrid.

The labelFunction is also avilable on <mx:List> an example of using it can be found in the livedocs.

Comments

cool example.. go labelFunction!


Thanks for that - been wondering how to do this for a little while.


Thank you! Very helpful - I'm dealing with that exact problem (returning complex XML from a web service) and you have saved the day.


Thank you! You example helped me solve a problem when returning a HTTPService over ActionScript.

Using mx.utils.ArrayUtil.toArray I can convert ProxyObject to ArrayColection.

Thank you! :D


This works to display the correct value, but sorting by double clicking in the header no longer works. Is there a fix for that ?


We have extended the Adobe DataGrid to create a really powerful component that provides all of the datagrid features, in addition to filter, paging, footers, select all, print, excel export, pdf export, please feel free to check out http://www.flexicious.com!


 
Page Render Time:188