简体   繁体   English

在ExtJS 4的tabpanel中,如何在单击选项卡时捕获tabchange事件?

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

For each tab in the tabpanel, I have one floating panel located in a fixed spot. 对于tabpanel中的每个选项卡,我有一个浮动面板位于固定位置。 When I switch the tabs, I need to bring the corresponding floating panel to the front. 当我切换标签时,我需要将相应的浮动面板放在前面。 However, the tabchange event is fired only the first time. 但是,tabchange事件仅在第一次触发时触发。 How can I catch this tabchange or other similar event when I click a tab? 单击选项卡时,如何捕获此tabchange或其他类似事件?

Alexey, Here is my example code with more specific questions: Alexey,这是我的示例代码,其中包含更具体的问题:

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 }
        }
    });
});

The "tabchange" message just prints once. “tabchange”消息只打印一次。 What I really want is to show the message "beforeshow on ..." everytime I click a tab. 我真正想要的是每次单击选项卡时都会显示“before before ...”。

Leoh, you are the man! 小姐,你是男人! It turned out the problem in my code is the "fn". 事实证明我的代码中的问题是“fn”。 The following code works perfectly by removing "fn", "scope", and "single". 通过删除“fn”,“scope”和“single”,以下代码可以完美地运行。 I don't know why though. 我不知道为什么。 Can anybody explain? 谁能解释一下?

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");
        }
    }
  });
});

Please attached a function to event tabchange 请附加一个功能到事件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);
            }
        }
    });
});

Live Demo Here 现场演示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM