flex4 Tree默认打开指定项并选中指定节点

来源:互联网 发布:mac 代理服务器 编辑:程序博客网 时间:2024/05/17 08:03
<mx:Tree id="tree" change="tree_changeHandler(event)" dataProvider="{zxljd}" width="300" height="100%" labelField="@name" showRoot="false" creationComplete="tree1_creationCompleteHandler(event)"></mx:Tree><fx:Script><![CDATA[[Bindable]private var zxljd:XML = getZxljd();//动态生成xml作为tree数据源public  function getZxljd():XML{var treexml:XML = new XML(<tree/>);for(var i:int=(new Date().fullYear-7); i<=(new Date().fullYear); i++){var yearxml:XML = new XML(<year/>);yearxml.@name = i + "年";for(var j:int=1; j<13; j++){var monthxml:XML = new XML(<month/>);monthxml.@name = j + "月";yearxml.appendChild(monthxml);}treexml.appendChild(yearxml);}return treexml;}protected function tree1_creationCompleteHandler(event:FlexEvent):void{//打开2006年这个节点(不要用openItems方法, 否则会影响下面设置默认选中效果)tree.expandChildrenOf(zxljd.year.(@name == "2006年")[0], true);//设置默认选中五月tree.selectedItem = (zxljd.year.(@name == "2006年").month.(@name == "5月")[0]) as XML;}protected function tree_changeHandler(event:ListEvent):void{trace(tree.selectedItem.toXMLString());}]]></fx:Script>

原创粉丝点击