Flex基于XML数据源国家省市三级联动效果(中英文完整版)
时间:2010-09-27 来源:莱卡斯
从网上找到一个国家省市三级联动的XML数据,但杯具的是没有程序,于是自己帮着补充下,另外这个XML数据全是全,但格式不统一啊,我只用了中国和美国的。这两个国家的数据倒还完整。

2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600"
3 creationComplete="init()">
4 <mx:Script>
5 <![CDATA[
6 import mx.collections.XMLListCollection;
7 /**
8 * 功能:国家省市三级联动,如果要实现其他的联动功能,可以参考这个,Enjoy It ^_^.
9 * @author liekkas
10 * @date 2010.09.27
11 * */
12 [Bindable]private var nationList:XMLListCollection; //国家数据提供源
13 [Bindable]private var stateList:XMLListCollection; //省数据提供源
14 [Bindable]private var cityList:XMLListCollection; //市数据提供源
15 [Embed(source="fun/1.gif")]private var fun1:Class;
16 [Embed(source="fun/3.gif")]private var fun3:Class;
17
18 private function init():void
19 {
20 if(!nationList)
21 nationList = new XMLListCollection();
22 if(!stateList)
23 stateList = new XMLListCollection();
24 if(!cityList)
25 cityList = new XMLListCollection();
26 chose("chinese"); //默认为中文
27 }
28
29 //语言选择
30 private function chose(type:String):void
31 {
32 if('chinese' == type){
33 funImage.source = fun3;
34 initData(locationCn);
35 }else{
36 funImage.source = fun1;
37 initData(locationEn);
38 }
39 }
40
41 //初始化数据
42 private function initData(location:XML):void
43 {
44 nationList.source = location.CountryRegion;
45 onNationChange();
46 onStateChange();
47 }
48
49 //国家联动事件
50 private function onNationChange():void
51 {
52 var x:XML = nation.selectedItem as XML;
53 //转换时由于联动关系,x值可能为空,此时用list的第一个数据填充
54 if(x)
55 stateList.source = x.State;
56 else
57 stateList.source = nationList.source[0].State;
58
59 //如果没有相关的数据则隐藏该combobox
60 if(stateList.length == 0)
61 state.visible = state.includeInLayout = false;
62 else
63 state.visible = state.includeInLayout = true;
64 onStateChange();
65 }
66
67 //省联动事件
68 private function onStateChange():void
69 {
70 var x:XML = state.selectedItem as XML;
71 //转换时由于联动关系,x值可能为空,此时用list的第一个数据填充
72 if(x)
73 cityList.source = x.City;
74 else
75 cityList.source = stateList.source[0].City;
76
77 //如果没有相关的数据则隐藏该combobox
78 if(cityList.length == 0)
79 city.visible = city.includeInLayout = false;
80 else
81 city.visible = city.includeInLayout = true;
82 }
83
84 //名字过长,截取显示
85 private function shortName(x:XML):String
86 {
87 var name:String = x.@Name;
88 return name.length > 13?name.substr(0,13)+"...":name;
89 }
90 ]]>
91 </mx:Script>
92 <mx:XML id="locationCn" format="e4x" source="chinese.xml"/>
93 <mx:XML id="locationEn" format="e4x" source="english.xml"/>
94 <mx:HBox horizontalAlign="center">
95 <mx:RadioButton id="chinese" label="中文版" groupName="language" selected="true"
96 click="chose('chinese')"/>
97 <mx:RadioButton id="english" label="英文版" groupName="language"
98 click="chose('english')"/>
99 </mx:HBox>
100 <mx:HBox horizontalAlign="center">
101 <mx:ComboBox id="nation" width="120" dataProvider="{nationList}"
102 change="onNationChange()" labelFunction="{shortName}"/>
103 <mx:ComboBox id="state" width="120" dataProvider="{stateList}"
104 change="onStateChange()" labelFunction="{shortName}"/>
105 <mx:ComboBox id="city" width="120" dataProvider="{cityList}"
106 labelFunction="{shortName}"/>
107 </mx:HBox>
108 <mx:Spacer height="50"/>
109 <mx:Image id="funImage"/>
110 </mx:Application>
111
同步发送到9ria论坛上去,顺便挣银子...杯具
http://bbs.9ria.com/thread-64453-1-1.html
相关阅读 更多 +