原文同步至: 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();}//搜尋過濾後,生產新的xmlprivate 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>