设置搜索Flex 4.6 XML搜索、匹配示例,完整代码

改章节朋友在广东喝咖啡的时候突然想到的...这两天就有想写几篇关于设置搜索的博客,所以回家到之后就奋笔疾书的写出来发表了

    原文同步至:  http://www.waylau.com/flex-4-6-xml-search-sample-matches/

    

    

效果见图

    

初始化界面

    

设置和搜索

    

输入“设置”,停止搜索、匹配后界面

    

设置和搜索

    

上面是代码

    每日一道理
微笑着,去唱生活的歌谣,不要埋怨生活给予了太多的磨难,不必抱怨生命中有太多的曲折。大海如果失去了巨浪的翻滚,就会失去雄浑;沙漠如果失去了飞沙的狂舞,就会失去壮观。人生如果仅去求得两点一线的一帆风顺,生命也就失去了存在的意义。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   minWidth="955" minHeight="600" creationComplete="init(event)">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			import mx.utils.StringUtil;
			import spark.events.TextOperationEvent;

			public var rawList:XML = 
				<apps name="应用程序" >
				  <item name="关于"
				  		icon="plugins/about_002/assets/icons/about_48.png" 
				  		iconSmall="plugins/about_002/assets/icons/about_24.png" 
				  		moduleUrl="plugins/about_002/Ahout_002.swf"
				  		version="1.0"  date="2013-5-13"  author="way" type="plugin"
				  		description="关于动力管理中心的一个说明" />

				 <item name="程序管理" 
				  		icon="plugins/appManager_001/assets/icons/apps_48.png" 
				  		iconSmall="plugins/appManager_001/assets/icons/apps_24.png"
				  		moduleUrl="plugins/appManager_001/AppManager_001.swf"
				  		version="1.0"  date="2013-3-5"  author="way" type="plugin"  resizable="false"
				  		description="对系统应用,及用户自定义应用停止管理"
				  		shortCutCreated="true"/>

				  <item name="导航设置" 
				  		icon="plugins/navigatorSetting_001/assets/icons/gears_48.png" 
				  		iconSmall="plugins/navigatorSetting_001/assets/icons/gears_24.png" 
				  		moduleUrl="plugins/navigatorSetting_001/NavigatorSetting_001.swf"
				  		version="1.0"  date="2013-3-13" author="way" type="plugin" resizable="false"
						description="对导航停止设置"/>
				  <item name="主题设置" 
				  		icon="plugins/themeSetting_001/assets/icons/icon_48.png" 
				  		iconSmall="plugins/themeSetting_001/assets/icons/icon_24.png" 
				  		moduleUrl="plugins/themeSetting_001/ThemeSetting_001.swf"
				  		version="1.0"  date="2013-3-5" author="way" type="plugin" resizable="false"
				  		description="对系统的主题、款式停止设置"/>
			    </apps>
				;     

			[Bindable]
			public var rawListShow:XML = null;  //搜索过滤后的数据

			protected function init(event:FlexEvent):void
			{
				getData();
			}

			protected function textinput1_changeHandler(event:TextOperationEvent):void
			{
				getData();
			}

			//初始化数据
			private function getData():void{
				if(StringUtil.trim(textInput.text) == ""){
					rawListShow = rawList ;

				}else{
					createNewXml(textInput.text,rawList);
				}

				/* 打开或关闭指定项目下的所有树项目。如果设置 dataProvider 之后当即调用 expandChildrenOf(),
				则您可能看不到准确的行为。您应该等待对组件停止验证或调用 validateNow() 方法 */
				tree1.validateNow();

				expandtree();
			}

			//搜索过滤后,生产新的xml
			private function createNewXml(searchString:String, xml:XML):void{
				rawListShow =<apps  name="应用程序" />;

				for(var i:int = 0; i<xml.children().length(); i++)
				{
					var itemXml:XML = xml.child("item")[i];
					if(isInStr(searchString,itemXml.@name.toString())){   
						rawListShow.appendChild(itemXml);
					}
				}
			}

			//判断search_str是否在str内 
			public function isInStr(search_str:String , str:String):Boolean{    
				var num:int= str.indexOf(search_str);
				if(num>-1){
					return true;
				}else{
					return false;
				}
			}

			//开展树
			private function expandtree():void {
				for each(var item:XML in this.tree1.dataProvider)
				this.tree1.expandChildrenOf(item,true);
			}

		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 该例子由waylau.com提供-->
	</fx:Declarations>
	<s:TextInput prompt="请输入要搜索的字段"  x="10" y="10" 
				 change="textinput1_changeHandler(event)" id="textInput"/>
	<mx:Tree id="tree1" dataProvider="{rawListShow}" 
			 labelField="@name" width="200" height="300" x="10" y="40">
	</mx:Tree>

	<s:Label text="更多例子 请存眷  waylau.com" x="10" y="360"/>
</s:Application>

文章结束给大家分享下程序员的一些笑话语录: 问路
有一个驾驶热气球的人发现他迷路了。他降低了飞行的高度,并认出了地面 上的一个人。他继续下降高度并对着那个人大叫,“打扰一下,你能告诉我我 在哪吗?”
下面那个人说:“是的。你在热气球里啊,盘旋在 30 英尺的空中”。
热气球上的人说:“你一定是在 IT 部门做技术工作”。
“没错”,地面上的人说到,“你是怎么知道的?”
“呵呵”,热气球上的人说,“你告诉我的每件事在技术上都是对的,但对都没 有用”。
地面上的人说,“你一定是管理层的人”。
“没错”,热气球上的人说,“可是你是怎么知道的?”
“呵呵”,地面上的那人说到,“你不知道你在哪里,你也不知道你要去哪,你 总希望我能帮你。你现在和我们刚见面时还在原来那个地方,但现在却是我 错了”。

posted @ 2013-05-22 21:59  坚固66  阅读(192)  评论(0编辑  收藏  举报