Flex XML搜索、匹配

时间:2023-03-09 01:42:17
Flex XML搜索、匹配

-

 <?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()"> <fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.utils.StringUtil; import spark.events.TextOperationEvent; public var listshow:XML=
<apps name="计算机课程">
<item name="算法与数据结构"/>
<item name="分布式计算"/>
<item name="计算机网络"/>
<item name="分布式系统"/>
</apps>;
[Bindable]
public var listnew:XML=null; //搜索过滤后的数据 public function init():void
{
getData();
}
protected function textinput_change(event:TextOperationEvent):void
{
getData();
}
protected function getData():void
{
//删除字符串的开头末尾的空格
if(StringUtil.trim(textinput.text)=="")
{
listnew=listshow;
}else{
createNewXML(textinput.text,listshow);
}
//打开或关闭指定项目下的所有树项目。如果设置 dataProvider 之后立即调用 expandChildrenOf()
//则您可能看不到正确的行为。您应该等待对组件进行验证或调用 validateNow()方法
tree.validateNow();
expandtree();
}
//搜索过滤产生新的XML
private function createNewXML(str:String,xml:XML):void
{
listnew=<apps name="计算机课程" />;
for(var i:int=0;i<xml.children().length();i++)
{
var itemxml:XML=xml.child("item")[i];
if(isInStr(str,itemxml.@name.toString()))
listnew.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;
}
protected function expandtree():void
{
for each(var itemxml in this.tree.dataProvider)
this.tree.expandChildrenOf(itemxml,true);
}
]]>
</fx:Script>
<s:Panel width="767" height="361" title="演示实例">
<s:TextInput id="textinput" x="10" y="10" width="174" height="32"
change="textinput_change(event)" prompt="请输入要搜索的关键词"/>
<mx:Tree id="tree" x="10" y="50" width="174" height="208" dataProvider="{listnew}"
labelField="@name"/>
<s:Label x="14" y="263" height="28" text="更多例子 请关注:www.baidu.com"/>
</s:Panel>
</s:Application>

效果图:

Flex XML搜索、匹配

搜索结果:

Flex XML搜索、匹配

参考文献:http://www.oschina.net/code/snippet_853151_21508

Flex XML搜索、匹配

Flex XML搜索、匹配