Extjs - 树面板根据条件折叠和展开

Extjs - Tree Panel collapse and expand based on condition

我想在 extjs 4.2.1

中根据 tree.panel 中的条件折叠或展开节点
tree.on("beforeitemexpand",function(node) {
    if (booleanFlag === true) {
        //allow to expand
    } else {
        //donot allow to expand
    }
});

我试过 beforeitemExpand 然后 return false 如果 booleanFlag 为假,但它不起作用。

事件 "beforeitemexpand" 在 Extjs 4.2.1 中似乎有一个错误,它并不理想,但您可以使用 "beforeitemclick" 和 "beforeitemdblclick" 来实现您想要的功能:

Ext.application({
name: 'Fiddle',

launch: function () {

    var enableHomeExpand = false;
    var enableBookExpand = false;

    var store = Ext.create('Ext.data.TreeStore', {
        root: {

            children: [{
                text: 'homework',
                expanded: false,
                children: [{
                    text: 'book report',
                    children: [{
                        text: 'test',
                        leaf: true
                    }, {
                        text: 'test 2',
                        leaf: true
                    }]
                }, {
                    text: 'algebra',
                    leaf: true
                }]
            }, {
                text: 'homework',

                children: [{
                    text: 'book report',
                    children: [{
                        text: 'test',
                        leaf: true
                    }, {
                        text: 'test 2',
                        leaf: true
                    }]
                }]
            }]
        }
    });

    var handleClick = function (node,rec,item){
                       if ((rec.data.text =="book report")&&(enableBookExpand)){
                           return true;
                       }
                       if ((rec.data.text =="homework")&&(enableHomeExpand)){
                            return true;
                        }
                        return false;
                    }

    var treepanel = Ext.create('Ext.tree.Panel', {
        title: 'Simple Tree',
        width: 400,
        height: 200,
        store: store,
        rootVisible: false,
        renderTo: Ext.getBody(),
        listeners:{
            beforeitemdblclick: handleClick,
            beforeitemclick: handleClick
        },
        buttons:[{
            text:'Enable Expand "homework"',
            handler: function(){ enableHomeExpand = true; }
        },
        {
            text:'Enable Expand "book report"',
            handler: function(){ enableBookExpand = true; }
        }]

    });

}
});

这是FIDDLE