在 ExtJS 4 的选项卡面板中,如何在单击选项卡时捕获选项卡更改事件

In the tabpanel of ExtJS 4, how to catch the tabchange event anytime I click a tab?

本文关键字:选项 单击 事件 ExtJS      更新时间:2023-09-26

对于选项卡面板中的每个选项卡,我都有一个位于固定位置的浮动面板。当我切换选项卡时,我需要将相应的浮动面板带到前面。但是,选项卡更改事件仅在第一次触发。单击选项卡时如何捕获此选项卡更改或其他类似事件?

阿列克谢,这是我的示例代码,其中包含更具体的问题:

Ext.onReady(function() {
    panel1 = Ext.create('Ext.panel.Panel', {
        title: 'title1', width: 800, height: 50,
        listeners: { 'beforeshow': { fn: function() { 
            console.log("beforeshow on title1"); 
        }, scope: this, single: true } }
    });
    panel2 = Ext.create('Ext.panel.Panel', {
        title: 'title2', width: 800, height: 50,
        listeners: { 'beforeshow': { fn: function() { 
            console.log("beforeshow on title2"); 
        }, scope: this, single: true } }
    });
    panel3 = Ext.create('Ext.panel.Panel', {
        title: 'title3', width: 800, height: 50,
        listeners: { 'beforeshow': { fn: function() { 
            console.log("beforeshow on title3"); 
        }, scope: this, single: true } }
    });
    var tabpanel = Ext.createWidget('tabpanel', {
        renderTo: document.body,
        activeTab: 0,
        width: 800, height: 50, plain: true,
        items: [panel1, panel2, panel3],
        listeners: {
            'tabchange': { fn: function() {
                 console.log("tabchange");
            }, scope: this, single: true }
        }
    });
});

"选项卡更改"消息仅打印一次。我真正想要的是显示消息"之前显示......"每次我点击一个选项卡时。

里奥,你就是男人!事实证明,我的代码中的问题是"fn"。以下代码通过删除"fn"、"scope"和"single"完美运行。我不知道为什么。谁能解释一下?

Ext.onReady(function() {
    panel1 = Ext.create('Ext.panel.Panel', {
      title: 'title1', width: 800, height: 50,
      listeners: { 'beforeshow': function() { console.log("beforeshow on title1"); } }
    });
    panel2 = Ext.create('Ext.panel.Panel', {
      title: 'title2', width: 800, height: 50,
      listeners: { 'beforeshow': function() { console.log("beforeshow on title2"); } }
    });
    panel3 = Ext.create('Ext.panel.Panel', {
      title: 'title3', width: 800, height: 50,
      listeners: { 'beforeshow': function() { console.log("beforeshow on title3"); } }
    });
  var tabpanel = Ext.createWidget('tabpanel', {
    renderTo: document.body,
    activeTab: 0,
    width: 800,
    height: 50,
    plain: true,
    items: [panel1, panel2, panel3],
    listeners: {
        'tabchange': function() {
             console.log("tabchange");
        }
    }
  });
});

请将函数附加到事件选项卡更改

Ext.onReady(function () {
    panel1 = Ext.create('Ext.panel.Panel', {
        title: 'title1'

    });
    panel2 = Ext.create('Ext.panel.Panel', {
        title: 'title2'
    });
    panel3 = Ext.create('Ext.panel.Panel', {
        title: 'title3'
    });
    var tabpanel = Ext.createWidget('tabpanel', {
        renderTo: document.body,
        activeTab: 0,
        width: 800,
        height: 50,
        plain: true,
        items: [panel1, panel2, panel3],
        listeners: {
            'tabchange': function (tabPanel, tab) {
                console.log(tabPanel.id + ' ' + tab.id);
            }
        }
    });
});

现场演示在这里

您可以尝试在选项卡更改后显示的面板上使用 beforeshow 事件