【问题标题】:Need simple working example of a tree filter for extjs 3.4需要 extjs 3.4 的树过滤器的简单工作示例
【发布时间】:2015-07-02 16:20:17
【问题描述】:

如何将树形过滤器应用于树形面板?

我知道这似乎是一个简单的问题,但我是 extjs 的新手,处理一些复杂的代码(从我的角度来看)。我不知道如何将树过滤器应用于树面板。我在树形过滤器here 上找到了文档,但我不知道如何在树形面板上使用它。

这是我的树面板的代码:

{                                       
    xtype: 'treepanel',
    loader: new Ext.tree.TreeLoader(),
    itemId:'TreePanelThisUserCanSee',
    rootVisible: false,
    border: true,
    autoScroll: true,
    hideCollapseTool: true,
    animate: false,
    getSelectedArray: function () {
        var selNodes = this.getChecked();
        var msg = '';
        var assignArray = new Array();
        Ext.each(selNodes, function(node) {
            if(!node.disabled) {
                if(msg.length > 0){
                    msg += ', ';
                }
                msg += node.id;
                assignArray[assignArray.length] = node.id;
            }
        });
        return assignArray;
    },
    root: new Ext.tree.AsyncTreeNode({
        text: 'Locations',
        draggable: false,
        id: 'root*node',
        leaf: false,
        expanded: true,
        expandable: false,
        children: [] // must have this to programatically add
    }),
    listeners: {
        'checkchange': function(node, checked) { 
        if(checked) {
            if( node.hasChildNodes() ) {
                node.expand(false, false);
                node.eachChild(function () {
                    this.ui.toggleCheck(true);
                    if(this.hasChildNodes()) {
                        this.eachChild(function () {
                            this.ui.toggleCheck(true);
                        });
                    }
                });
            }
        } else {
            node.eachChild(function () {
            this.ui.toggleCheck(false);
            if(this.hasChildNodes()) {
                this.eachChild(function () {
                    this.ui.toggleCheck(false); 
                });
            }                                                       
        });
    }
}
}
}

【问题讨论】:

    标签: javascript extjs tree extjs3


    【解决方案1】:

    读取this thread 并使用过滤器检查远程树的this example

    您也可以检查此代码:

    var config = {
            readOnly: false,
            isExpand: false,
            mode: 'local',
            treeFilter:  new Ext.tree.TreeFilter(this.getTree(), {
                autoClear: true,
                filterBy : function(fn, scope, startNode){
                    startNode = startNode || this.tree.root;
                    if(this.autoClear){
                        this.clear();
                    }
                    var found = {};
                    var af = this.filtered, rv = this.reverse;
                    var f = function(n){
                        if(n == startNode){
                            return true;
                        }
                        if(af[n.id]){
                            return false;
                        }
                        var m = fn.call(scope || n, n);
                        if(!m || rv){
                            af[n.id] = n;
                            //                        n.ui.hide();
                            //                        return false;
                            return true;
                        }
    
                        found[n.id] = n;
                        return true;
                    };
    
                    startNode.cascade(f);
    
                    for(var idf in found){
                        if(typeof idf != "function"){
                            var curFoundItem = found[idf];
                            var p = curFoundItem.parentNode;
                            while(p){
                                delete af[p.id];
                                p = p.parentNode;
                            }
                        }
                    }
    
                    for(var id in af){
                        if(typeof id != "function"){
                            var n = af[id];
                            n.ui.hide();
                        }
                    }
    
                    //startNode.cascade(f2);
    
                    if(this.remove){
                        for(var id in af){
                            if(typeof id != "function"){
                                var n = af[id];
                                if(n && n.parentNode){
                                    n.parentNode.removeChild(n);
                                }
                            }
                        }
                    }
                }
            }),
    
            listeners: {
                scope: this,
                beforequery: function(){
                  return false;
                },
                keyup: {
                    fn: function(field, key){
                        if(!this.isExpand)
                            this.expand();
                        var value = field.getRawValue();
                        if(Ext.isEmpty(value) && !Ext.isEmpty(field.treeFilter)){
                            field.treeFilter.clear();
                            return;
                        }
                        var re = new RegExp('' + value + '', 'i');
                        var tree = field.getTree();
                        tree.expandAll();
                        field.treeFilter.filter(re);
                    },
                    buffer: 250
                }
            }
        }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 2011-09-03
      • 2023-03-25
      相关资源
      最近更新 更多