【问题标题】:Get Extjs grid on which context menu item is clicked获取单击上下文菜单项的 Extjs 网格
【发布时间】:2016-10-10 10:25:10
【问题描述】:

我有一个网格,并且在网格上下文菜单上我正在调用一个函数,如下所示-

在网格上下文菜单中,我添加了以下项目

{ text: 'Preview', handler: 'PreviewGrid', scope: cnt, };

在控制器中-

previewGrid: function (contextMenuItem) {    
        // Here I am getting the item i.e. contextmenu item.
        // But I want here is grid on which there was right click
}

我尝试使用item.ownerCt.up('grid')

但它不起作用。

任何帮助将不胜感激。

【问题讨论】:

  • 我没有看到您的代码在哪里以及如何实例化上下文菜单,但是为什么您在该过程中删除了参数,然后又来寻求我们的帮助以再次检索它们? ExtJS 自己的itemcontextmenu ( this , record , item , index , e , eOpts ) event 正在提供所需的信息。
  • 我这样做了,但我无法在其中获得网格。添加菜单项时我做错了吗?
  • 你的问题没有意义,这里没有上下文。

标签: javascript extjs grid contextmenu extjs6


【解决方案1】:

示例代码:https://fiddle.sencha.com/#fiddle/1i9o

Ext.application({
    name: 'Fiddle',

    launch: function() {
        var grid = Ext.create('Ext.grid.Panel', {
            renderTo: Ext.getBody(),
            width: 400,
            height: 500,
            title: 'itemcontextmenu',

            store: {
                fields: ['name', 'email', 'phone'],
                data: [{
                    'name': 'Lisa',
                    "email": "lisa@simpsons.com",
                    "phone": "555-111-1224"
                }, {
                    'name': 'Bart',
                    "email": "bart@simpsons.com",
                    "phone": "555-222-1234"
                }, {
                    'name': 'Homer',
                    "email": "homer@simpsons.com",
                    "phone": "555-222-1244"
                }, {
                    'name': 'Marge',
                    "email": "marge@simpsons.com",
                    "phone": ""
                }]
            },
            columns: [{
                text: 'Name',
                dataIndex: 'name',
                flex: 1
            }]
        });

        var contextMenu = Ext.create('Ext.menu.Menu', {
            width: 200,
            items: [{
                text: 'Preview',
                handler: function() {
                    var record = grid ? grid.getSelection()[0] : null;
                    if (!record) {
                        return;
                    }

                    alert(record.get('name'));
                }
            }]
        });

        grid.on("itemcontextmenu", function(grid, record, item, index, e) {
            e.stopEvent();
            contextMenu.showAt(e.getXY());
        });
    }
});

【讨论】:

  • 好榜样 madreason
【解决方案2】:

这对我有用。

在网格监听器中:

listeners: {

  itemcontextmenu: function (grid, record, item, index, e) {

     var contextMenu = Ext.create('Ext.menu.Menu', {
         height: 200,
         width: 250,
         items: [{
                text:'Preview', 
                handler: function () {
                    //code...
                }
         }]
     });
    e.stopEvent();
    contextMenu.showAt(e.getXY());
  }
}

【讨论】:

  • 每次右键单击项目时都会创建菜单。
  • 坦率地说,我还没有找到另一种方法来做到这一点。效果很好。示例:fiddle.sencha.com/#fiddle/1i9g
  • 我在answer中重构了你的代码
猜你喜欢
  • 2013-05-25
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 2021-05-12
  • 2013-11-12
  • 1970-01-01
  • 2017-05-13
  • 2023-03-05
相关资源
最近更新 更多