Flex tree 模仿Eclipse帮助 过滤

 

今天看到本站有人帖的java中操作tree数据源的例子,想起来自己以前写过的flex中过滤tree节点的代码,大概功能是: 对提供的数据源(xml类型,还有另一个版本是ArrayCollection类型数据源的,但并未整合到一起)进行递归匹配,匹配到的颜色标红,匹配 不到的,如果子项匹配的也保留,否则过滤掉。 用的时候需要外部传入过滤的key,(可以在外部放一个TextInput,值改变就调用这个组件的set key 函数) 大家看看。希望有人会用到,我项目中亲测的,欢迎牛人提意见!

 

    
        
            import org.loong.component.util.ComponentUtil;
            import mx.core.UIComponent;
            import mx.managers.DragManager;
            import mx.events.DragEvent;
            import org.loong.component.util.VUtil;
            import mx.utils.StringUtil;
            import mx.utils.ObjectUtil;
            import mx.collections.XMLListCollection;
            import mx.controls.Alert;
            import mx.controls.TextInput;
            import mx.events.ListEvent;
            private var _treeData:XMLList; //存储当前Tree数据(可能是筛选后的)
            private var FINALDATA:XMLList; //存储真正的Tree数据
            [Bindable]
            private var _treeLabel:String;

            public function set clickTree(_func:Function):void
            {
                callLater(function():void
                    {
                        autoCompleteTree.addEventListener(MouseEvent.MOUSE_DOWN, _func);
                    });
            }

            public function set dragCompleteVtree(_dragfunc:Function):void
            {
                callLater(function():void
                    {
                        autoCompleteTree.addEventListener(DragEvent.DRAG_COMPLETE, _dragfunc);
                    });
            }

            public function set labelField(_labelField:String):void
            {
                _treeLabel = _labelField;
            }

            public function get getVtree():Tree
            {
                return autoCompleteTree;
            }

            public function set dataProvider(_data:XML):void
            {
                callLater(function():void
                    {
                        if (_data == null)
                            return;
                        autoCompleteTree.dataProvider = _data;
                        FINALDATA = getDataProvider(autoCompleteTree).copy()
                        openTree();
                    });

            }

            public function set defaultSelectedIndex(_index:int):void
            {
                callLater(function():void
                    {
                        autoCompleteTree.selectedIndex = _index;
                    });

            }

            //关键字改变
            public function set key(_key:String):void
            {
                if (StringUtil.trim(_key) == "")
                {
                    callLater(function():void
                        {
                            autoCompleteTree.dataProvider = FINALDATA.copy();
                            openTree();
                        });
                    return;
                }

                _treeData = FINALDATA.copy();
                filterTree(_key, _treeData);
                callLater(function():void
                    {
                        removeNotMatchNode(_treeData);
                        callLater(function():void
                            {
                                autoCompleteTree.dataProvider = _treeData;
                                openTree();
                            });
                    });

            }

            //过滤不匹配节点
            private function removeNotMatchNode(xmllist:XMLList):void
            {
                for (var i:int = 0; i < xmllist.length(); i++)
                {
                    if (xmllist[i].@isMatching == undefined)
                    {
                        delete xmllist[i];
                        i ;
                    }
                    if (xmllist[i].children() != null)
                    {
                        removeNotMatchNode(xmllist[i].children());
                    }
                }
            }

            //展开tree节点
            public function openTree():void
            {
                callLater(function():void
                    {
                        VUtil.changeAllTree(autoCompleteTree, false);
                    });
            }

            //过滤tree
            private function filterTree(key:String, _treeData:XMLList):Boolean
            {

                var containsKey:Boolean = false;
                for each (var element:XML in _treeData)
                {
                    if (compare(element[_treeLabel], key)) //包含此节点,那高亮显示当前节点,并且继续向下递归,
                    {
                        containsKey = true;
                        element.@isMatching = true;
                        if (element.children() != null)
                        {
                            filterTree(key, element.children());
                        }
                    }
                    else if (element.children() != null && filterTree(key, element.children()))
                    { //不包含此节点,但是此节点有子节点,看子节点是否有需要留下的,如果有,那显示这个节点,否则都去掉

                        containsKey = true;
                        element.@isMatching = false;
                    }
                }
                return containsKey;


            }


            //忽略大小写比较_source字符串是否包含_key
            private function compare(_source:String, _key:String):Boolean
            {
                return _source.toLowerCase().indexOf(_key.toLowerCase()) == -1 ? false : true;
            }

            /**
             * 以XML形式返回树的数据源对象
             * */
            private function getDataProvider(_tree:Tree):XMLList
            {
                var o:XMLListCollection = _tree.dataProvider as XMLListCollection;
                var xmllist:XMLList = o.source as XMLList;
                return xmllist;
            }
            public function set showRoot(_bool:Boolean):void{
                callLater(function():void
                    {
                        autoCompleteTree.showRoot = _bool;
                    });
            }

            //双击展开树节点
            private function doubleClickVTree(event:MouseEvent):void
            {
                ComponentUtil.ocSelectTree(event);
		//这个函数只是展开树节点
            }
        
    
    
        
    

你可能感兴趣的:(Flex,tree,过滤)