2017-08-02 49 views
0

在我的extjs網格中,當我右鍵單擊時,我調用上下文菜單。當我點擊其中一個項目時,我想在控制器中啓動一個方法。這一切工作順利,問題是我想通過這個被稱爲從控制器方法的父網格。有人可以告訴我如何做到這一點?extjs contextmenu單擊,將父網格作爲參數傳遞給控制器​​方法

這是我的上下文菜單

Ext.define('Example.ContextMenuTradematch', { 
xtype: 'contextMenuTradematch', 
extend: 'Ext.menu.Menu', 
items: [ 
    { 
     text: 'Match Trade', 
     iconCls: 'greenIcon', 
     listeners: { 
      click: {      
       fn: 'onMatchTrade', 
       params: { 
        param1: this 
       } 
      } 
     } 
    }, 
    { 
     text: 'Delete Trade', 
     iconCls: 'deleteIcon', 
     listeners: { 
      click: 'onDeleteTrade' 
     } 
    } 
] 

});

那麼這是我的控制器方法

onMatchTrade: function (event, target, options) { 
    debugger; 
    var me = this; 

如何訪問該事件源於電網?

--and我這是怎麼添加上下文菜單電網

    title: 'Tradematch Results: UNMATCHED', 
       xtype: 'grid', 
       itemId: 'gridUnmatchedId', 
       ui: 'featuredpanel-framed', 
       cls: 'custom-grid', 
       margin: '0px 10px 0px 10px', 
       flex: 2, 
       width: '100%', 
       bind: { 
        store: '{myTM_ResultsStore}' 
       }, 
       listeners: { 
        itemcontextmenu: 'showContextMenuTradematch' 
       }, 

,這是控制器如何增加它...

getContextMenu: function (cMenu) { 
    if (!this.contextMenu) { 
     debugger; 
     this.contextMenu = this.getView().add({ xtype: cMenu }); 
    } 
    return this.contextMenu; 
}, 

showContextMenuTradematch: function (view, rec, node, index, e) { 
    e.stopEvent(); 
    e.stopEvent(); 
    debugger; 
    this.getContextMenu('contextMenuTradematch1').show().setPagePosition(e.getXY()); 
    return false; 
}, 
+0

如何網格創建到控制器?沒有足夠的上下文信息。 –

回答

1

做到這一點,最簡單的方法是當您創建Example.ContextMenuTradematch實例時 - 假設您從itemcontextmenu偵聽器中執行 - 則可以將引用傳遞給網格。

itemcontextmenu: function (grid, record, item) { 
    // code to create your menu 
    // probably something like: 
    if (!grid.contextMenu) { 
      grid.contextMenu = Ext.create('Example.ContextMenuTradematch', { 
       ownerGrid: grid 
      }); 
    } 

    grid.contextMenu.showBy(item); 
} 

如果onMatchTrade是通過點擊一個Ext.menu.Item實例觸發那麼它的簽名是:

onMatchTrade: function (item, e) { 
    var menu = item.up('menu'), 
     grid = menu.ownerGrid; 

    console.log(grid); 
} 

有很多猜測這裏。如果這不是您創建菜單或調用方法的方式,那麼添加小提琴將有助於解決問題。

這裏是爲模板,用小提琴:https://fiddle.sencha.com/#view/editor&fiddle/24fc

+0

我添加了更多關於如何將菜單添加到網格的細節。我選擇這種方式的唯一原因是因爲這是我能夠弄清楚的唯一方法。我打開了一個更好的方式 – solarissf

+0

我試過你這樣做的方式,我想我會用它...似乎更清潔。一個問題是,上下文菜單顯示自己遠離點擊按鈕。我通過以下代碼解決了這個問題:this.contextMenu = this.getView()。add({xtype:'contextMenuTradematch2'})。show()。setPagePosition(e.getXY());任何想法如何我可以用你的例子做同樣的事情? – solarissf

+0

是的,只需使用'grid.contextMenu.showAt(e.getX(),e.​​getY())'。我已經更新了小提琴。 –

相關問題