Flex中如何通过ComboBox对Tree中介点进行选择展开处理的例子

By Minidxer | December 25, 2008

接下来的例子演示了Flex中如何通过ComboBox,对Tree中介点进行选择展开处理。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white">
  6.  
  7.     <mx:Script>
  8.         <![CDATA[
  9.             import mx.events.ListEvent;
  10.  
  11.             private function comboBox_change(evt:ListEvent):void {
  12.                 var team:String = ComboBox(evt.currentTarget).selectedItem.@label;
  13.                 var node:XMLList = mlb.league.division.team.(@label == team);
  14.                 expandParents(node[0]);
  15.  
  16.                 tree.selectedItem = node[0];
  17.                 var idx:int = tree.getItemIndex(node[0]);
  18.                 tree.scrollToIndex(idx);
  19.             }
  20.  
  21.             private function expandParents(node:XML):void {
  22.                 if (node && !tree.isItemOpen(node)) {
  23.                     tree.expandItem(node, true);
  24.                     expandParents(node.parent());
  25.                 }
  26.             }
  27.         ]]>
  28.     </mx:Script>
  29.  
  30.     <mx:XML id="mlb" source="mlb.xml" />
  31.  
  32.     <mx:ApplicationControlBar dock="true">
  33.         <mx:ComboBox id="comboBox"
  34.                 prompt="Please select a team..."
  35.                 dataProvider="{mlb.league.division.team}"
  36.                 labelField="@label"
  37.                 change="comboBox_change(event);" />
  38.     </mx:ApplicationControlBar>
  39.  
  40.     <mx:Tree id="tree"
  41.             dataProvider="{mlb}"
  42.             labelField="@label"
  43.             showRoot="false"
  44.             width="300"
  45.             rowCount="8" />
  46.  
  47. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Tree | No Comments » | 726 views Tags: , , , ,

Search Posts