如何在extjs 6中单击而不是鼠标悬停显示子菜单?

时间:2015-09-17 08:04:23

标签: javascript extjs menu extjs6 extjs6-classic

在extjs 6菜单中,如何设置子菜单在点击时显示而不是鼠标悬停?我没有找到任何配置来设置子菜单的显示触发器,并且使用按钮作为菜单项也不起作用。

var myMenu = Ext.create({
        xtype: 'menu',
        items: [{
            text: 'Menu Item 1',
            menu: {
                items: [{
                    text: 'sub-Menu Item 1'
                }, {
                    text: 'sub-Menu Item 2'
                }]
            }
        }, {
            text: 'Menu Item 2',
            menu: {
                items: [{
                    text: 'sub-Menu Item 1'
                }, {
                    text: 'sub-Menu Item 2'
                }]
            }
        }]
    });

1 个答案:

答案 0 :(得分:2)

您必须覆盖菜单onMouseOveronClick方法,以防止扩展菜单。您还需要在菜单中添加ignoreParentClicks配置。

它看起来像:

Ext.define('Ext.o.menu.Menu', {
    override : 'Ext.menu.Menu',
    onClick: function(e) {
        var me = this,
            type = e.type,
            item,
            clickResult,
            iskeyEvent = type === 'keydown';

        if (me.disabled) {
            e.stopEvent();
            return;
        }
        item = me.getItemFromEvent(e);
        if (item && item.isMenuItem) {
            if (!item.menu || !me.ignoreParentClicks) {
                clickResult = item.onClick(e);
            } else {
                e.stopEvent();
            }

            // SPACE and ENTER invokes the menu
            if (item.menu && clickResult !== false) {
                item.expandMenu(e, 0);
            }
        }
        // Click event may be fired without an item, so we need a second check
        if (!item || item.disabled) {
            item = undefined;
        }
        me.fireEvent('click', me, item, e);
    },
    onMouseOver: function(e) {
        var me = this,
            fromEl = e.getRelatedTarget(),
            mouseEnter = !me.el.contains(fromEl),
            item = me.getItemFromEvent(e),
            parentMenu = me.parentMenu,
            ownerCmp = me.ownerCmp;

        if (mouseEnter && parentMenu) {
            parentMenu.setActiveItem(ownerCmp);
            ownerCmp.cancelDeferHide();
            parentMenu.mouseMonitor.mouseenter();
        }

        if (me.disabled) {
            return;
        }

        // Do not activate the item if the mouseover was within the item, and it's already active
        if (item) {
            if (!item.containsFocus) {
                item.focus();
            }
            // This will not be needed here!
            //if (item.expandMenu) {
                //item.expandMenu(e);
            //}
        }
        if (mouseEnter) {
            me.fireEvent('mouseenter', me, e);
        }
        me.fireEvent('mouseover', me, item, e);
    }
});

检查这个小提琴:https://fiddle.sencha.com/#fiddle/u1m