即使在 DataGrid 的空白区域中右键单击,Dojo DataGrid 上下文菜单 onRowContextMenu 也会显示

标签 dojo contextmenu right-click dojox.grid dojox.grid.datagrid

我有一个包含项目的 DataGrid。当您右键单击其中一行时,会显示一个 Dojo 上下文菜单,其中包含删除该行的选项。如果您尝试右键单击 DataGrid 的空白区域,则不会显示上下文菜单...但是,如果您首先右键单击一行,然后单击“取消”菜单选项(不执行任何操作)或者如果您左键单击页面上的其他位置(隐藏上下文菜单),然后右键单击 DataGrid 的空白区域,将显示上下文菜单,如果您单击上下文菜单中的“删除项目”选项,它将删除您右键单击的最后一个项目。

为什么当您右键单击 DataGrid 的空白区域时才允许显示上下文菜单,但只有在您已经右键单击了其中的项目之后数据网格?

如有任何提示,我们将不胜感激。这是到目前为止我的代码:

var selectedItem;  // This has to be declared "globally" outside of any functions

function onRowContextMenuFunc(e) {
    grid5_rowMenu.bindDomNode(e.grid.domNode);
    selectedItem = e.grid.getItem(e.rowIndex);
}

function gridRowContextMenu_onClick(e) {
    store3.deleteItem(selectedItem);
}

.

<div dojoType="dijit.Menu" id="grid5_rowMenu" jsId="grid5_rowMenu" style="display: none;">
    <div dojoType="dijit.MenuItem" onClick="gridRowContextMenu_onClick">Delete</div>
    <div dojoType="dijit.MenuItem">Cancel</div>
</div>

.

<div id="grid" dojoType="dojox.grid.DataGrid" jsId="grid5" store="store3" structure="layoutStructure" rowsPerPage="40" onRowContextMenu="onRowContextMenuFunc"></div>

最佳答案

嗯,我不太确定为什么只有在第一次右键单击某个项目后才允许右键单击空白区域时显示上下文菜单,但我确实想出了一个解决方法来解决我的根本问题: 右键单击数据网格中的行项目,然后单击关闭以隐藏上下文菜单,然后右键单击数据网格的空白区域并选择菜单项会导致第一个右键单击的 rowIndex待通过

这是我的代码;我希望这对将来遇到同样问题的人有所帮助:

 var selectedItem;

 function onRowContextMenu(e) {
      grid5_rowMenu.bindDomNode(e.grid.domNode);
      selectedItem = e.grid.getItem(e.rowIndex);
 }

 function gridRowContextMenuExecute(task) {
      if((task == "remove") && (selectedItem != null)) {
           store3.deleteItem(selectedItem);
      }
      else {
           selectedItem = null;
      }
 }

.

 <div dojoType="dijit.Menu" id="grid5_rowMenu" jsId="grid5_rowMenu" style="display: none;" onBlur="gridRowContextMenuExecute('cancel')">
      <div dojoType="dijit.MenuItem" onMouseDown="gridRowContextMenuExecute('remove')">Remove from Transaction</div>
      <div dojoType="dijit.MenuItem">Cancel</div>
 </div>

.

 <div id="grid" dojoType="dojox.grid.DataGrid" jsId="grid5" store="store3" structure="layoutStructure" rowsPerPage="40" onRowContextMenu="onRowContextMenu"></div>

关于即使在 DataGrid 的空白区域中右键单击,Dojo DataGrid 上下文菜单 onRowContextMenu 也会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8187693/

相关文章:

javascript - 放置完成时的拖放事件

internationalization - 如何以最简单的方式使用 Dojo i18n

contextmenu - 如何从上下文菜单中删除 "open with Gitkraken"?

jquery - 如何向 jQuery 添加禁用右键单击的异常(exception)?

java - 为什么右键单击不适用于 java 应用程序?

javascript - Dojo 从 javascript 设置验证

CSS 样式化 dojo 中的提示工具提示

Android 上下文菜单 - 未选择任何内容

.net - 如何向 System.Windows.Forms.MenuItem 添加图标?

javascript - 使用 jQuery 如何获取目标元素上的右键单击坐标