javascript ExtJs 4 上的 TreeNode 单击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5719414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:07:33  来源:igfitidea点击:

TreeNode click event on ExtJs 4

javascriptextjsextjs4

提问by Mario Cesar

I'm using ExtJS 4 (beta 3) and I have a TreePanel that is my kind of navigation menu. It is something like this:

我正在使用 ExtJS 4(测试版 3)并且我有一个 TreePanel,它是我的导航菜单。它是这样的:

  • Jobs
    • Add Job
    • List All Jobs
  • ...
    • ...
    • ...
  • 工作
    • 添加工作
    • 列出所有工作
  • ...
    • ...
    • ...

(this will be made on a permission system base, but that's another story)

(这将在权限系统基础上进行,但这是另一回事)

On ExtJS 3, do something when i clicked "Add Job" was as simple as adding

在 ExtJS 3 上,当我点击“添加工作”时做一些事情就像添加一样简单

...
leaf:true,
listeners:{
click:function(n){
       //my code...
   }
}
...

to the root children elements.

到根子元素。

Now It's not that simple. The closer i got was with (on the treepanel)

现在事情没那么简单。我离得越近(在树板上)

listeners:{
    click : {
             element : 'el',
             fn : function(eve, elem, obj){
                   console.log(node);
                   console.log(elem);
                   console.log(obj);
                  }
    }
}

So, maybe i'm just a noob, maybe i have already a strong hatred for ExtJS, maybe is just a problem in this beta version, but...

所以,也许我只是一个菜鸟,也许我已经对 ExtJS 有强烈的仇恨,也许只是这个 beta 版本的一个问题,但是......

How do I add a listener to the click event on the tree nodes? (the Select event won't do what i need)

如何为树节点上的单击事件添加侦听器?(选择事件不会做我需要的)

Thank you guys.

感谢你们。

EDIT: Currently testing with this, and it's not working.

编辑:目前正在对此进行测试,但它不起作用。

 ... = Ext.create('Ext.tree.TreePanel', {
                        region      : 'west',
                        collapsible : false,
                        title       : 'ITMI',
                        width       : 220,
                        margins     : '5 5 5 5',
                        cmargins    : '5 5 5 5',
                        hideHeaders : true,
                        useArrows   : true,
                        rootVisible : false,
                        headers: [{
                                xtype    : 'treeheader',
                                text     : 'Nome',
                                flex     : 1,
                                dataIndex: 'nome'
                            }],
                        store: store,
                        listeners:{
                            itemclick: function(n){
                                console.info(n);
                            }
                        }
    ...

EDIT 2:The itemclick event now works (on EXJS 4 final), It still doesn't solve my problem. I'd Like to call a specific function when i call each treenode. Before it was really easy. Now i can't figure it out.

编辑 2:itemclick 事件现在可以工作(在 EXJS 4 final 上),它仍然没有解决我的问题。当我调用每个树节点时,我想调用一个特定的函数。在这之前真的很容易。现在我想不通了。

回答by Egy Mohammad Erdin

in ext4 beta3 (maybe in final release too)... there is no longer clickevent....
this has changed to itemclickmore info

在 ext4 beta3 中(也可能在最终版本中)......不再有click事件......
这已更改为itemclick更多信息

var tree = Ext.create('Ext.tree.Panel', {
    store: store,
    renderTo: Ext.getBody(),
    height: 300,
    width: 250,
    title: 'Files',

    listeners:{
        itemclick: function(n){
            console.info(n);
        }
    }

});

回答by Mario Cesar

So, It may help some people who may be struggling with the same issue I did then.

因此,它可能会帮助一些可能与我当时遇到的相同问题而苦苦挣扎的人。

The "itemclick" event is the way to handle leafs clicks, and it didn't work then for reasons I don't remember.

“itemclick”事件是处理叶子点击的方法,但由于我不记得的原因,它当时不起作用。

I accomplished what I needed by splitting the config I had in the database, something like

我通过拆分我在数据库中的配置来完成我需要的东西,比如

controllerName|functionName

and then call this code on the handler of the "itemclick:

然后在“itemclick”的处理程序上调用此代码:

this.getController(ctr)[fn]();  

where ctris the controllerNameand fnis the functionName. This could easily be done with eval, but I prefer not to.

其中ctrcontrollerNamefnfunctionName。这可以通过 eval 轻松完成,但我不想这样做。

回答by Andy

I could not get itemclick to fire with IE (fine in Chrome). I modified my code to use 'checkchange' and it works fine.

我无法使用 IE 触发 itemclick(在 Chrome 中很好)。我修改了我的代码以使用“checkchange”并且它工作正常。