【问题标题】:How to get the parent of a menu?如何获取菜单的父级?
【发布时间】:2011-10-17 18:43:00
【问题描述】:

我正在尝试获取链接菜单的组件。 看看:

    Ext.create('Ext.Button', {
    id: 'MyButton',
    text: 'Click me',
    renderTo: Ext.getBody(),
    menuAlign: 'tl-bl',
    menu: {
        itemId: 'MyMenu',        
        forceLayout: true,
        items:
        [
            {
                text  : 'Option 1',
                itemId: 'MyItemMenu1'
            }, {
                text  : 'Option 2',
                itemId: 'MyItemMenu2'
            }, {
                text   : 'Get the parent!',
                itemId : 'MyItemMenu3',
                handler: function(){
                    
                    // Get the item menu.
                    var MyItemMenu3 = this; 
                    alert(MyItemMenu3.getItemId()); 
                    
                    // Get the menu.
                    var MyMenu = MyItemMenu3.ownerCt; 
                    alert(MyMenu.getItemId());
                    
                    // Try to get the button.
                    var MyButton = MyMenu.ownerCt; 
                    alert(MyButton);                    
                    
                    // Returns:                    
                    // 'MyItemMenu3'
                    // 'MyMenu'
                    // undefined                 
                }
            }
        ]
    }
});

在线示例: http://jsfiddle.net/RobertoSchuster/mGLVF/

有什么想法吗?

【问题讨论】:

  • 我自己正在学习 EXT,所以我不太确定发生了什么,但我认为我能够通过这种方式获得它:console.log(MyMenu.floatParent.id);
  • 完美!有用!谢谢。
  • 好的,我会把它作为答案提交 :) 很高兴它对你有用!

标签: extjs extjs4 extjs3


【解决方案1】:

我自己正在学习 EXT,所以我不太确定发生了什么,但我认为我能够通过这种方式获得它:console.log(MyMenu.floatParent.id);

【讨论】:

  • 是的,我认为简单的 up() 调用就可以解决问题。但没有 :) 谢谢你的回答......很有魅力
【解决方案2】:

在我们的 ExtJS 4 项目中,我们最终只是修补了 AbstractComponent 上的 up() 函数

Ext.AbstractComponent.override({
    up: function(selector) {
        var result = this.ownerCt||this.floatParent;
        if (selector) {
            for (; result; result = result.ownerCt||result.floatParent) {
                if (Ext.ComponentQuery.is(result, selector)) {
                    return result;
                }
            }
        }
        return result;
    }
});

这使得up() 沿着ownerCt 链向上走,但如果ownerCt 未定义,它会查找floatParent

现在您可以致电cmp.up('some-selector');,即使它是一个菜单或菜单项。

【讨论】:

    【解决方案3】:

    试试这个:

    Ext.getCmp('My-Button').menu.refOwner
    

    【讨论】:

      【解决方案4】:
      var MyButton = MyItemMenu3.up('button');
      

      或者对于 ExtJS3:

      var MyButton = MyItemMenu3.findParentByType('button');
      

      【讨论】:

      • 我都试过了,但总是返回未定义。它适用于 MyMenu.floatParent。 (:谢谢。
      【解决方案5】:

      至少在最新版本的 ExtJS (4.2) 中,每个 Ext.menu.Item 都有一个可以访问父菜单的parentMenu 属性。您可以将其子类化为自定义菜单项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-20
        • 2011-02-12
        • 2012-10-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多