Harshrossi
Harshrossi

Reputation: 287

Ext.menu.Menu (Context Menu) is displayed only once on right click - Ext JS 4.2.1

I have a tree grid and I am displaying a context menu on mouse right click. I am creating the menu in the 'itemContextMenu' event of the tree grid view.

Problem is that the context menu gets displayed only for the first time when I right click on any of the nodes in the Tree grid. Rest of the times it displays a small box (menu with no items in it) which sticks on the screen itself.

I donno what is wrong. I just want a simple context menu to work..

Help would be much appreciated.

Here is my code:

Tree Panel:

Ext.define('MyApp.view.MainPanel', {
extend: 'Ext.panel.Panel',

height: 361,
width: 681,
layout: {
    type: 'absolute'
},
title: 'Main Panel',
initComponent: function() {
    var me = this;

    Ext.applyIf(me, {
        items: [
            {
                xtype: 'treepanel',
                height: 335,
                width: 475,
                title: 'Tree Grid Panel',
                store: 'TreeStore',
                displayField: 'kstand',
                rootVisible: false,
                useArrows: true,
                viewConfig: {
                    listeners: {
                        itemcontextmenu: {
                            fn: me.onViewItemContextMenu,
                            scope: me
                        }
                    }
                },
                columns: [
                    {
                        xtype: 'treecolumn',
                        width: 237,
                        sortable: false,
                        dataIndex: 'kstand',
                        text: 'Kstand',
                        flex: 1
                    },
                    {
                        xtype: 'gridcolumn',
                        sortable: false,
                        dataIndex: 'name',
                        text: 'Name'
                    }
                ]
            }
        ]
    });

    me.callParent(arguments);
},

onViewItemContextMenu: function(dataview, record, item, index, e, eOpts) {
    e.stopEvent();
    var contextMenu = Ext.create('MyApp.view.TreeContextMenu');
    contextMenu.showAt(e.getXY());
}

});

Context Menu:

Ext.define('MyApp.view.TreeContextMenu', {
extend: 'Ext.menu.Menu',

hidden: true,
hideMode: 'display',
id: 'TreeContextMenu',
width: 138,
frameHeader: false,
initComponent: function() {
    var me = this;
    Ext.applyIf(me, {
        items: [
            {
                xtype: 'menuitem',
                id: 'AddTask',
                icon: '/Images/Addblue.png',
                text: 'Add Task',
                tooltip: 'Add Task'
            },
            {
                xtype: 'menuitem',
                id: 'EditTask',
                icon: '/Images/edit.png',
                text: 'Edit Task',
                tooltip: 'Edit selected task'
            }
        ]
    });
    me.callParent(arguments);
}

});

enter image description here

enter image description here

Upvotes: 1

Views: 5600

Answers (1)

Evan Trimboli
Evan Trimboli

Reputation: 30082

Because you're creating a new menu instance every time, which means: a) Memory leak b) Duplicate component id

So, remove the id from the class definition, you don't need it. Secondly, only create the menu once:

onViewItemContextMenu: function(dataview, record, item, index, e, eOpts) {
    e.stopEvent();
    if (!this.menu) {
        this.menu = Ext.create('MyApp.view.TreeContextMenu');
    }
    this.menu.showAt(e.getXY());
}

Upvotes: 3

Related Questions