Extjs - 树面板根据条件

时间:2018-05-29 07:50:11

标签: javascript extjs4.2

我想根据extjs 4.2.1中tree.panel中的条件折叠或展开节点

tree.on("beforeitemexpand",function(node) {
    if (booleanFlag === true) {
        //allow to expand
    } else {
        //donot allow to expand
    }
});

我已经尝试过beforeitemExpand,如果booleanFlag为false,则返回false,但它不起作用。

1 个答案:

答案 0 :(得分:1)

事件" 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