How do I attach an event handler to a panel in extJS?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 02:19:02
Brian Moeskau

[For anyone else reading this, I went through a fairly thorough explanation of this already here.]

You are missing a few key concepts:

click is not a valid Panel event, so adding a click handler on a Panel will do nothing (this is the issue in most of the code you posted above).

In this code:

var menuItem1 = new Ext.Panel({
   ...
}); 
content.body

You copy-pasted from another answer, but incorrectly. content in the other code referenced the Panel that was created -- it is a variable. In this code you created the Panel as menuItem1 but then are trying to reference it as content?

Please re-read my previous explanation about how rendering works in the other answer I gave you. You must either add a Panel to a container that renders it, or render it directly (via the renderTo config). If you do neither, the Panel will not show up.

Using jQuery's delegate function is not the proper approach with Ext JS components.

try this:

var menuItem1 = new Ext.Panel({
    id: 'panelStart',
    title: 'Start',
    html: 'This is the start page.',
    cls:'menuItem',
    listeners: {
        afterrender: function (comp) {
            var element = comp.getEl();
            element.on('click', function() {
                alert('ok')
            });
        }
    }
}); 

If you want to listen to events on Ext.Elements inside of a panel, you use the element property when calling addListener or passing in listeners config, instead of waiting for the afterrender event just to set the listeners

var menuItem1 = new Ext.Panel({
    id: 'panelStart',
    title: 'Start',
    html: 'This is the start page.',
    cls:'menuItem',
    listeners: {
        click: {
           element: 'el', // could be 'body', or any other Ext.Elements 
                          // that are available from the component
           fn: function() {}
        }
    }
}); 

Or a more simple approach:

    listeners: { 'expand': {fn: adminSelected }} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!