需要一个简单的工作示例,用于extjs 3.4

时间:2015-07-01 13:28:42

标签: javascript extjs tree extjs3

如何在树状面板上应用树状过滤器?

我知道这可能看起来像一个简单的问题,但我不熟悉处理一些复杂代码的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); 
                });
            }                                                       
        });
    }
}
}
}

1 个答案:

答案 0 :(得分:3)

阅读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
            }
        }
    }

我希望这会对你有帮助!