5

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

    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/

任何的想法?

4

5 回答 5

6

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

于 2011-08-03T15:14:10.260 回答
3

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

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');即使它是菜单或菜单项,您也可以调用。

于 2012-04-19T19:39:36.470 回答
1

试试这个:

Ext.getCmp('My-Button').menu.refOwner
于 2011-08-03T02:20:14.137 回答
1
var MyButton = MyItemMenu3.up('button');

或者对于 ExtJS3:

var MyButton = MyItemMenu3.findParentByType('button');
于 2011-08-03T05:07:22.260 回答
0

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

于 2014-02-18T15:59:58.073 回答