Flex renderer data inside a panel
I have a page (below) that has a datagrid that lists "item"'s returned from an XML file (below) and when it loads it created a page in the viewstack for each item it finds (working) it also renders a page (below) inside each panel but i am having issues passing the data. Each page renders and loads but with the FIRST result of the XML data instead of each page having its own data they all have the same as the first page.
HomePage.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%" height="100%"
creationComplete="init()"
xmlns:software="com.xd.components.pages.software.*"
xmlns:pages="com.xd.components.pages.*"
xmlns:renderers="com.xd.components.renderers.*">
<mx:Script>
<![CDATA[
import mx.core.Application;
import com.xd.components.renderers.PageListRenderer;
import mx.containers.Panel;
import mx.core.Container;
import com.xd.components.renderers.SoftwareListRenderer;
import mx.utils.ObjectProxy;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.containers.VBox;
[Bindable] public var httpData:Object;
[Bindable] public var selectedItem:String;
private function init():void {
//get data
httpService.send();
}
//private const MAX_CHILDREN:uint = 5;
private function viewstack_addChild(name:String):void {
var p:Panel = new Panel();
p.id = name;
p.name = name;
p.title = name;
p.percentWidth = 100;
p.percentHeight = 100;
var randColor:uint = Math.random() * 0xFFFFFF;
p.setStyle("backgroundColor", randColor);
var pR:PageListRenderer = new PageListRenderer();
//var data:Object;
//Do something to get the data to be displayed;
//data = httpService.lastResult.item;
pR.data = httpData;
p.addChild(pR);
myViewStack.addChild(p);
}
private function httpResult_handler(evt:ResultEvent):void {
if (evt.result.software.item) {
httpData = XML(evt.result).descendants("item");
var item:Object = httpData;
for each(item in httpData) {
viewstack_addChild(item.name);
}
}
}
public function switchView(viewId:String):void
{
var container:Container = Container(myViewStack.getChildByName(viewId));
selectedItem = viewId;
if (container != null)
{
myViewStack.selectedChild = container;
}
}
]]>
</mx:Script>
<mx:HTTPService id="httpService" url="data/Software.xml" resultFormat="e4x" result="httpResult_handler(event)" fault="Alert.show('XML Data Error')" />
<mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<mx:HBox width="100%" height="100%">
<mx:VBox width="30%" height="100%">
<mx:Panel borderThicknessLeft="0" borderThicknessRight="0" borderThicknessBottom="0" width="100%" height="100%" title="Software List:">
<mx:DataGrid id="myDG"
dataProvider="{httpData}"
headerHeight="0"
editable="false"
width="100%" height="100%"
rowHeight="50"
itemClick="switchView(myDG.selectedItem.name);">
<mx:columns>
<mx:DataGridColumn itemRenderer="com.xd.components.rendere开发者_如何学Crs.SoftwareListRenderer" />
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</mx:VBox>
<mx:VBox width="70%" height="100%">
<mx:ViewStack id="myViewStack" width="100%" height="100%" hideEffect="WipUp" showEffect="WipeDown" creationPolicy="all">
<pages:NewsPage id="NewsPage" height="100%" width="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" />
<software:ShowTracker id="ShowTracker" height="100%" width="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" />
</mx:ViewStack>
</mx:VBox>
</mx:HBox>
</mx:VBox>
</mx:Canvas>
PageListRenderer.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox
height="100%"
width="100%"
xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.core.Application;
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:VBox height="70%" width="100%" horizontalAlign="center" verticalAlign="middle">
<mx:Image source="{data.image}" width="90%" height="90%"/>
</mx:VBox>
<mx:VBox height="30%" width="100%" horizontalAlign="left" verticalAlign="top">
<mx:Label width="100%" text="Name / Version:" color="#FFFFFF"/>
<mx:Label width="100%" text="{data.name}" color="#FFAE00"/>
<mx:Label width="100%" text="{data.version}" color="#FFAE00"/>
<mx:Label width="100%" text="{data.longdesc}" color="#FFFFFF"/>
<mx:Button click="installApp()" label="Install Application" horizontalCenter="0" verticalCenter="0" width="160" height="52" id="install" visible="false"/>
<mx:Button click="launchApp()" label="Launch Application" horizontalCenter="0" verticalCenter="0" width="160" height="52" id="launch" visible="false"/>
</mx:VBox>
</mx:VBox>
</mx:HBox>
Software.xml
<software>
<item>
<name>ShowTracker</name>
<version>1.0</version>
<shortdesc>TV Show Tracker.</shortdesc>
<image></image>
<longdesc>ShowTracker allows you to manage all the TV Shows you watch. I helps you keep track of what episode you are upto for each series you are currently watching</longdesc>
<appid>com.showtracker</appid>
<pubid>EB6DA1B683461BF1C71FEC13E276982941DF167B.1</pubid>
<appurl>http://www.nitcom.com.au/showtracker/downloads/ShowTracker-v1.air</appurl>
<runversion>1.5</runversion>
</item>
<item>
<name>Test2</name>
<version>1</version>
<shortdesc>test desc #2</shortdesc>
<image>test1.jpg</image>
<longdesc>test1.jpg</longdesc>
<appid>com.showtracker</appid>
<pubid>EB6DA1B683461BF1C71FEC13E276982941DF167B.1</pubid>
<appurl>http://www.nitcom.com.au/showtracker/downloads/ShowTracker-v1.air</appurl>
<runversion>1.5</runversion>
</item>
<item>
<name>Test #3</name>
<version>1</version>
<shortdesc>test desc #3</shortdesc>
<image>test1.jpg</image>
<longdesc>test1.jpg</longdesc>
<appid>com.showtracker</appid>
<pubid>EB6DA1B683461BF1C71FEC13E276982941DF167B.1</pubid>
<appurl>http://www.nitcom.com.au/showtracker/downloads/ShowTracker-v1.air</appurl>
<runversion>1.5</runversion>
</item>
</software>
dont know if this is causing the problem but it looks like this :
private function httpResult_handler(evt:ResultEvent):void {
if (evt.result.software.item) {
httpData = XML(evt.result).descendants("item");
var item:Object = httpData;
for each(item in httpData) {
viewstack_addChild(item.name);
}
}
}
needs to be this:
private function httpResult_handler(evt:ResultEvent) :void {
if(evt.result.software.item){
var httpData:XMLList = XML(evt.result).descendants("item");
for each(var item:Object in httpData){
viewstack_addChild(item.name);
}
}
}
and really you should pass the view stack the whole item and let it take what it needs instead of sending it individual properties.
精彩评论