我如何知道所选节点是否是树中的叶子? (ExtJS 4)

时间:2012-04-02 13:54:39

标签: javascript extjs

我有一个树面板,我想将一个动作与这棵树的叶子上的点击相关联。我设法将一个关联到树元素上的每次点击,但我不仅仅关注树叶。

这里是我的代码,我用非常简单的函数试了一下:(菜单是一个视图扩展树.Panel)

Ext.define('WSR.controller.MenuControl', {

    extend: 'Ext.app.Controller',

    views: [
        'Menu',
        'Browser'
    ],

    stores: [ 'UserStore' ],

    init: function() {

        this.control({
            'menu': {
                itemclick: this.onItemClicked
            }
        });
    },

    onItemClicked: function() {
        console.log('ItemClicked');
    }
});

有人帮我吗?

3 个答案:

答案 0 :(得分:2)

尝试:

Ext.define('WSR.controller.MenuControl', {

extend: 'Ext.app.Controller',

views: [
    'Menu',
    'Browser'
],

refs:[
    {
        selector: 'Menu', //Suggest u to use a selector that is more "tightened" in case you have more trees of this type
        ref: 'mynicetree'
    }
],

stores: [ 'UserStore' ],

init: function() {

    this.control({
        'menu': {
            //I suggest u use selectionchange instead, itemclick won't work if the use navigate the tree nodes using keyboard, whereas selectionchange fit both
            selectionchange: this.loadStuff
        }
    });
},

loadStuff: function() {

    //Assuming you can only select one node at a time (typical use case)
    var myTreePanel = this.getMynicetree();
    var nodes = myTreePanel.getSelectionModel().selection();
    if(!Ext.isEmpty(nodes))
    {
        var selNode = nodes[0];
        if(selNode.get("leaf")) //Assuming your NodeInterface "leaf" property was assigned
        {
            //Its a leaf
        }

        //OR
        if(selNode.isLeaf())
        {
            //Its a leaf
        }

    }
}

});

HTH!

答案 1 :(得分:0)

如果您有节点ID,那么您可以尝试以下代码:

 taxonomyTreeLoader.on("load", function(){
            if(isQueryExecutable && singleParamDynamicQuery &&docTypeCodeDynamciQuery.length>0){
                var currentNode=taxonomyTree.getNodeById(docTypeNodeId);
                if(currentNode.isLeaf()){
                    taxonomyTree.getSelectionModel().select(currentNode);
                    taxonomyTree.fireEvent("click", currentNode);
                } 
            }   
        });    

答案 2 :(得分:0)

问题来自去年,但此解决方案可能对某人有帮助

init:function(){
    this.control({
        //Component listeners
        'menu': {
            itemclick: this.onItemClicked
       }
    });

    this.application.on({
        //Event handlers
    });

},

onItemClicked: function(tree, record) {
    if(record.isLeaf()){
        alert('related: '+"\n"+record.getId());
        console.log(record.getId());
    } 
}