You need to use a result handler function. Here is some sample code:
Sample code using HTTPService, e4x, handler function to populate a list
item
The DataGrid tag:
<mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>
The HTTPService tag:
<mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)"
fault="..../>
Script block declaration:
import mx.rpc.Events.ResultEvent;
[Bindable]private var _xlcMyListData:XMLListCollection;
var oRequest:Object = new Object();
oRequest.Arg1 = "value1";
service.send(oRequest);
Result Handler function:
private function onResult(oEvent:ResultEvent):void {
var xmlResult:XML = XML(event.result); //converts
result Object to XML. can also use "as" operator
var xlMyListData:XMLList = xmlResult.myListData; //depends on xml
format, is row data
_xlcMyListData = new XMLListCollection(xlMyListData); //wrap the
XMLList in a collection
trace(_xlcMyListData.toXMLString()); //so you can see
exactly how to specify dataField or build labelFunction
}//onResult
Tracy
________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of portuguesedanny
Sent: Sunday, May 20, 2007 2:03 PM
To: [email protected]
Subject: [flexcoders] Change hardcoded XML for dynamic XML source
Hi All,
Can anyone help? It is probably something very simple :-s
The following code works ok, but I need to change the source of the
XML data...
####################### CODE #####################
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> "
layout="vertical" creationComplete="initData()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var dataList:ArrayCollection ;
private function initData():void{
dataList= new ArrayCollection([
{name:"school A", city:"Paris"},
{name:"school B", city:"Pomona "},
{name:"School C", city:"Phillipsburg"},
{name:"School D", city:"Liverpool"},
{name:"School E", city:"Madrid"},
])
}
private function filterDemo():void{
dataList.filterFunction = searchDemo;
dataList.refresh();
}
private function searchDemo(item:Object):Boolean{
var isMatch:Boolean = false
if(item.name.toLowerCase().search(search.text.toLowerCase()) != -1){
isMatch = true
}
return isMatch;
}
private function clearSearch():void{
dataList.filterFunction = null;
dataList.refresh();
search.text = '';
}
]]>
</mx:Script>
<mx:Form>
<mx:FormItem label="Search Name" direction="horizontal">
<mx:TextInput id="search" change="filterDemo()" />
<mx:Button label="Clear Search" click="clearSearch()" />
</mx:FormItem>
</mx:Form>
<mx:DataGrid dataProvider="{dataList}" width="400" height="400">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name" />
<mx:DataGridColumn headerText="City" dataField="city" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
####################### /CODE #####################
As the XML data source in the above example is hardcoded, I want to
modify the code to get the data from a URL, but am having trouble..!
The following code is what I am having trouble with setting the XML
source...
Can anyone help, suggest how I might change
#################### CHANGE THIS ####################
private function initData():void{
dataList= new ArrayCollection([
{name:"school A", city:"Paris"},
{name:"school B", city:"Pomona "},
{name:"School C", city:"Phillipsburg"},
{name:"School D", city:"Liverpool"},
{name:"School E", city:"Madrid"},
])
}
#################### /CHANGE THIS ####################
#################### TO THIS #####################
private function initData():void{
dataList= new ArrayCollection(
<mx:HTTPService id="getStaffList"
url="http://192.168.0.84/amfphp/stafflist.php
<http://192.168.0.84/amfphp/stafflist.php> " />
)
}
#################### /TO THIS ####################
This should be the final code that works, but doesnt :-(
####################### CODE #####################
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> "
layout="vertical" creationComplete="getStaffList.send();">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var dataList:ArrayCollection ;
private function getStaffList():void{
dataList= new ArrayCollection;
}
private function filterDemo():void{
dataList.filterFunction = searchDemo;
dataList.refresh();
}
private function searchDemo(item:Object):Boolean{
var isMatch:Boolean = false
if(item.name.toLowerCase().search(search.text.toLowerCase()) != -1){
isMatch = true
}
return isMatch;
}
private function clearSearch():void{
dataList.filterFunction = null;
dataList.refresh();
search.text = '';
}
]]>
</mx:Script>
<mx:HTTPService id="getStaffList"
url="http://192.168.0.84/amfphp/stafflist.php
<http://192.168.0.84/amfphp/stafflist.php> " />
<mx:Form>
<mx:FormItem label="Search" direction="horizontal">
<mx:TextInput id="search" change="filterDemo()" />
<mx:Button label="Clear Search" click="clearSearch()" />
</mx:FormItem>
</mx:Form>
<mx:DataGrid dataProvider="{dataList}" width="400" height="400">
<mx:columns>
<mx:DataGridColumn headerText="Staff Code"
dataField="staffcode" />
<mx:DataGridColumn headerText="Department"
dataField="department" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
####################### /CODE #####################
The above code should be getting it's XML data provided by:
<mx:HTTPService id="getStaffList"
url="http://192.168.0.84/amfphp/stafflist.php
<http://192.168.0.84/amfphp/stafflist.php> " />
If I brouwse to the stafflist.php file from a browser I get XML in a
valid format :-)
How do I get the data into my flex app???
####################### XML #####################
<stafflist>
<staffmember>
<staffid>30</staffid>
<firstname>sarina</firstname>
<surname>redmond</surname>
<staffcode>sre</staffcode>
<emailaddress>[EMAIL PROTECTED] <mailto:sre%40email.com> </emailaddress>
<department>science</department>
</staffmember>
<staffmember>
<staffid>27</staffid>
<firstname>jayson</firstname>
<surname>poole</surname>
<staffcode>jpo</staffcode>
<emailaddress>[EMAIL PROTECTED] <mailto:jpo%40email.com> </emailaddress>
<department>business studies</department>
</staffmember>
<staffmember>
<staffid>32</staffid>
<firstname>Karina</firstname>
<surname>Bishop</surname>
<staffcode>kbi</staffcode>
<emailaddress>[EMAIL PROTECTED] <mailto:kbi%40email.com> </emailaddress>
<department>reception</department>
</staffmember>
<staffmember>
<staffid>33</staffid>
<firstname>James</firstname>
<surname>Stuart</surname>
<staffcode>jst</staffcode>
<emailaddress>[EMAIL PROTECTED] <mailto:jst%40email.com> </emailaddress>
<department>PE</department>
</staffmember>
</stafflist>
####################### /XML #####################