This is really from one struggling newbie to other struggling newbies. This code is really just code from here: http://kmossman.blogspot.com/2007/03/blog-post.html that I adjusted to read from a xml file via HTTPService, using the xml file that was used in this code: http://blog.flexexamples.com/2007/11/29/opening-nodes-in-a-flex-tree-con trol-using-the-expanditem-method/ All the code and xml required to do a simple tree in a combo box example (pulling in external xml for the tree) is below. Just take the xml and pasted it into a file named mlb.xml. Past the main code into your main mxml file and past the component code into a component named TreeComboBox.mxml and you're done. brad //****************************** Main Code //****************** <?xml version="1.0"?> <mx:Application xmlns:local="*" xmlns:mx="http://www.adobe.com/2006/mxml"; creationComplete="loadXML()"> <mx:Script> <![CDATA[ import mx.collections.XMLListCollection; import mx.rpc.events.ResultEvent; import mx.rpc.http.mxml.HTTPService; public var xmlService:HTTPService = new HTTPService(); [Bindable] public var xmlResult:XML; [Bindable] public var xmlList:XMLList; [Bindable] public var xmlTeams:XMLListCollection; public function loadXML():void { xmlService.url = "mlb.xml" xmlService.resultFormat = "e4x"; xmlService.addEventListener(ResultEvent.RESULT, resultHandler); xmlService.send(); } public function resultHandler(event:ResultEvent):void { xmlResult = XML(event.result); //enable if you want to see raw xml on screen instead of in trace // lastResultValue.text = xmlResult.toXMLString(); trace(xmlResult.toXMLString()); xmlList = xmlResult.league; xmlTeams = new XMLListCollection(xmlList); } ]]> </mx:Script> <!-- enable if you want to see raw xml on screen --> <!-- <mx:TextArea x="10" y="244" width="374" height="102" id="lastResultValue"/> --> <mx:TextInput text="[EMAIL PROTECTED]"/> <local:TreeComboBox width="300" id="combo" labelField="@label" dataProvider="{xmlTeams}" </mx:Application> //********************************************************************** ***************************** Component Code //******************* <?xml version="1.0" encoding="utf-8"?> <mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml";> <mx:Script> <![CDATA[ import mx.events.FlexEvent; [Bindable] private var _label:String; [Bindable] public var treeSelectedItem:Object; public function updateLabel(event:*):void { _label = event.currentTarget.selectedItem[this.labelField]; treeSelectedItem = event.currentTarget.selectedItem; } override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { super.updateDisplayList(unscaledWidth, unscaledHeight); if(dropdown && _label != null){ text = _label; } } ]]> </mx:Script> <mx:dropdownFactory> <mx:Component> <mx:Tree change="outerDocument.updateLabel(event)" height="500" width="500" /> </mx:Component> </mx:dropdownFactory> </mx:ComboBox> //**************************** End of Component Code ********************************** //*****************Begin XML File************************ <?xml version="1.0" encoding="utf-8"?> <root> <league label="American League"> <division label="West"> <team label="Los Angeles" /> <team label="Seattle" /> <team label="Oakland" /> <team label="Texas" /> </division> <division label="Central"> <team label="Cleveland" /> <team label="Detroit" /> <team label="Minnesota" /> <team label="Chicago" /> <team label="Kansas City" /> </division> <division label="East"> <team label="Boston" /> <team label="New York" /> <team label="Toronto" /> <team label="Baltimore" /> <team label="Tampa Bay" /> </division> </league> </root> //*******************End XML File ************************* 原文:http://www.mail-archive.com/flexcoders@yahoogroups.com/msg85229.html
Tree in Combo Box example code (using HTTPService and XML)
最新推荐文章于 2024-10-18 18:49:38 发布