`

Ext 树.叶子查找与非叶子查找

 
阅读更多
ext的tree的filter即搜索功能,按照ext的API写有一个bug,当节点下有二级子节点的时候,filter其它节点,不论此节点是否满足filter条件,都会出现,搞了半天才将这个bug解决掉,现在记下来,以备以后用到。
原代码:

hiddenPkgs = [];
        tree.root.cascade(function(n) {
            if(!n.isLeaf()&& n.ui.ctNode.offsetHeight<3){
                n.ui.hide();
                hiddenPkgs.push(n);
            }
       });
bug主要出在这段控制隐藏的代码上,其中关键是n.ui.ctNode.offsetHeight<3,当节点有子节点的时候,他的n.ui.ctNode.offsetHeight就会大于3,所以会显示。修改这个bug,只需要修改这段代码即可。在源过滤条件后面加个条件,修改后的代码如下:
修改一:只支持对叶子的搜索,不支持对枝干的搜索
hiddenPkgs = [];
        tree.root.cascade(function(n) {
            if(!n.isLeaf()&& n.ui.ctNode.offsetHeight<3&& !re.test(n.text)){
                n.ui.hide();
                hiddenPkgs.push(n);
            }
            if(n.id!='root'){
                if(!n.isLeaf() && n.ui.ctNode.offsetHeight >= 3 && hasChild(n,re)==false&& !re.test(n.text)){
                    n.ui.hide();
                    hiddenPkgs.push(n);
                }
            }
        });
      
        function hasChild(n,re){
         var str=false;
         n.cascade(function(n1){
              if(n1.isLeaf() && re.test(n1.text)){
                  str = true;
                  return;
              }
          });
          return str;
       }
    }
修改二:支持对叶子、枝干的搜索。
var hiddenPkgs = [];
    var filter = new Ext.tree.TreeFilter(tree, {
        clearBlank: true,
          autoClear: true
    });
    function filterTree(){
      
          var text = members_name.getValue();
          Ext.each(hiddenPkgs, function(n){
            n.ui.show();
        });

        if(!text){
            filter.clear();        
            return;
        }

        tree.expandAll();
        var re = new RegExp(Ext.escapeRe(text), 'i');
     
        filter.filterBy(function(n){
             var textval = n.text;
            return !n.isLeaf() || re.test(n.text);
        });

        // hide empty packages that weren't filtered
        hiddenPkgs = [];
        tree.root.cascade(function(n) {
            if(!n.isLeaf()&& n.ui.ctNode.offsetHeight<3&& !re.test(n.text)){
                n.ui.hide();
                hiddenPkgs.push(n);
            }
            if(n.id!='root'){
                if(!n.isLeaf() && n.ui.ctNode.offsetHeight >= 3 && hasChild(n,re)==false&& !re.test(n.text)){
                    n.ui.hide();
                    hiddenPkgs.push(n);
                }
            }
        });
      
        function hasChild(n,re){
            var str=false;
            n.cascade(function(n1){
                 if(re.test(n1.text)){
                     str = true;
                     return;
                 }
             });
             return str;
       }
    }
修改二与修改一是有区别的,并且修改二不能取代修改一,因为,修改一也有应用点,如:如果只让用户对叶子进行搜索那么应该是当搜索枝干时应该没有结果,若用修改二则不很好,用修改一更合理
分享到:
评论
1 楼 Navee 2012-09-13  
抄来抄去,还是没有实质解决问题

相关推荐

Global site tag (gtag.js) - Google Analytics