为 ag-grid 单元格中的鼠标和键盘事件绑定事件侦听器

Bind event listener for mouse and keyboard events in ag-grid cells

我们使用 ag-grid,我需要控制基于鼠标和键盘事件生成上下文菜单项的方式。在这种情况下,如果按下 alt+右键单击(而不是仅右键单击),我需要添加一个可选的上下文菜单项。在我们这边简单地绑定事件监听器并没有帮助,因为上下文菜单项是在事件监听器被触发之前绑定的,所以我无法检查用户是否按下了它们。 任何建议将不胜感激。

根据文档 Configuring the Context Menu,您可以使用 gridOptions.getContextMenuItems 函数提供上下文菜单项。在这里您可以查看是否按下 ShiftCtrl

if(this.event.altKey === true) {
  result.push({name: 'Alt key is pressed', disabled: true});
}
if(this.event.shiftKey === true) {
  result.push({name: 'Shift key is pressed', disabled: true});
}

看看我创建的这个 plunk:Context Menu Example

根据您在单击鼠标右键时按下的键,一项正在添加到上下文菜单中。