View Full Version : DataGrid Dataprovider
ackerchez
08-03-2009, 06:53 PM
Hi All,
I have a datagird that is using XML returned from php as its provider (httpservice.lastResult).
In the XML structure I have something like this
<root>
<mealorder chain="burgerking">
<order date="01/20/2007" amount="40.00">
<order date="02/20/2007" amount="20.00">
<order date="03/20/2007" amount="30.00">
<order date="04/20/2007" amount="50.00">
<mostrecentorder date="04/20/2007" amount="50.00">
</mealorder>
</root>
I have successfully managed to get the first datagrid column to show the chains. Now when I want to get the other column to show the mostrecentorder attributes it comes up empty. I thought that If I am setting the datagrid dataprovider to getmeals.lastresult.children() then I should be able to use dot-syntax to get into the lower nodes of the xml returned but it doesnt seem to be working. I have tried ".mostrecentorder.@date", "..@date", ".@date" and nothing works. Can anyone weigh in here and help me get the right syntax or maybe offer a better way to go about doing this?
Thanks!
rebelheart
08-04-2009, 05:55 AM
Two sample xmls, one yours and one with a more convenient structure[resulting in flat-objects] are used. Note a couple of things
1- Your xml is not well formed,maybe an accident.
2- Your xml structure doesn't result in 'flat' objects, but in an array-of-arrays kind of nested structure, so notice that the second and third columns of the datagrid don't have proper display.Study the xml structures used here and also the result of the http calls by setting up breakpoints ,you will understand the difference.
3- A key thing to remember is the 'resultFormat' of the http service object. Play around with those values and see what happens. Default is 'object' and that works best for these kind of scenarios.
4- You could use columns with predefined headers and all, but I haven't done all that,just demonstrated the basic point.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" preinitialize="getdata()">
<mx:Script>
<![CDATA[
import mx.controls.dataGridClasses.DataGridColumn;
import mx.collections.ArrayCollection;
import mx.utils.ArrayUtil;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var ac1:ArrayCollection;
[Bindable]
private var ac2:ArrayCollection;
private function getdata():void
{
http1.send();
http2.send();
}
private function resulth1(e:ResultEvent):void
{
ac1= e.result.root.mealorder;
}
private function resulth2(e:ResultEvent):void
{
ac2 = http2.lastResult.countries.country;
}
private function faulth(e:FaultEvent):void
{
}
]]>
</mx:Script>
<mx:HTTPService id="http1" url="assets/data/samplexml.xml" result="resulth1(event)"
fault="faulth(event)" />
<mx:HTTPService id="http2" url="assets/data/countries.xml" result="resulth2(event)"
fault="faulth(event)" />
<mx:DataGrid dataProvider="{ac1}" />
<mx:DataGrid dataProvider="{ac2}" >
<mx:columns>
<mx:DataGridColumn dataField="name" />
<mx:DataGridColumn dataField="capital" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
Samplexml.xml:
<root>
<mealorder chain="burgerking">
<order date="01/20/2007" amount="40.00"/>
<order date="02/20/2007" amount="20.00"/>
<order date="03/20/2007" amount="30.00"/>
<order date="04/20/2007" amount="50.00"/>
<mostrecentorder date="04/20/2007" amount="50.00"/>
</mealorder>
<mealorder chain="mcdonalds">
<order date="01/20/2007" amount="40.00"/>
<order date="02/20/2007" amount="20.00"/>
<order date="03/20/2007" amount="30.00"/>
<order date="04/20/2007" amount="50.00"/>
<mostrecentorder date="04/20/2007" amount="50.00"/>
</mealorder>
</root>
countries.xml:
<countries>
<country>
<name>Iran</name>
<capital>Tehran</capital>
</country>
<country>
<name>Iraq</name>
<capital>Baghdad</capital>
</country>
<country>
<name>Afghanistan</name>
<capital>Kabul</capital>
</country>
</countries>
Hope this helps.
ackerchez
08-04-2009, 09:46 AM
Thanks a bunch, I got it working. I just changed the resultformat to an ArrayCollection instead of XML.
Much Appreciated!
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.