【问题标题】:How to access (enhanced-) grid cell data from a cell context menu?如何从单元格上下文菜单访问(增强)网格单元格数据?
【发布时间】:2011-11-03 15:38:44
【问题描述】:

我的 Web 应用程序基于 dojo 1.6.0。 我遇到的问题基本上基于事件处理程序和/或它们的利用率 在道场“dojox.grid.EnhancedGrid”库中。

我的应用程序包含一个带有大量行的 dojox 增强网格。 (100+)

此增强网格使用“cellMenu”插件来显示上下文菜单 右键单击每个网格单元。

我的目标是使用上下文菜单“智能”选择行。

例如:

用户右键单击位于“lastname”列中且值为“miller”的单元格。然后他单击上下文菜单中的“智能选择”。 然后,应用程序将遍历行数据并选择所有将“miller”作为“lastname”的行。 之后,用户将通过按下按钮对选定的行发起操作。

这是一个小的源代码示例,说明了使用上下文菜单可视化增强网格的声明性方法:

<table dojoType="dojox.grid.EnhancedGrid" plugins="{cellMenu:'myMenu'}">
<div id="myMenu" dojoType="dijit.Menu">
  <div id="mi1" dojoType="dijit.MenuItem">Do something with this cell</div>
  <div id="mi2" dojoType="dijit.MenuItem">Do something else with this cell</div>
</div>
<thead>
  definition of columns
</thead>
</table>

动作代码与 js-Files 中的可视化分开处理:

<script type="text/javascript">
dojo.addOnLoad(function(){
  dojo.connect(dijit.byId('mi1'),'onClick',function(event){ 
    //Use Data from the cell clicked to do something
  });
  dojo.connect(dijit.byId('mi2'),'onClick',function(event){
    //Use Data from the cell clicked to do something else
  });
});
</script>

我对 dojo 比较陌生,没有处理增强网格的经验。

所以我的问题如下:

当我在上下文菜单中单击“dijit.Menu”时,“onClick”事件 其中包含的“dijit.MenuItem”被触发。

在这个事件处理程序中,我需要在上下文菜单中读取“网格单元”的内容 已打开,但我没有(或目前不知道)获取对“网格单元”的引用的方法。

使用默认策略,我可能能够获得对 MenuItem 的引用,并可能从那里获得对菜单的引用,但我无法找到包含对“网格单元”或行/列 ID 的引用的属性使我能够访问单击的单元格。

由于上下文菜单可以对通过右键单击打开它们的“项目”做一些事情,我认为必须有一种方法(正如设计师所说的那样)来访问这个“项目”。

我还没有找到说明这一点的文档或示例,非常感谢您的帮助。

【问题讨论】:

  • 我认为上下文菜单的菜单项单击事件没有对单击元素的内置引用很奇怪。如果上下文菜单不能与单元格相关,那么上下文菜单(尤其是增强网格的单元格上下文菜单)的意义何在?

标签: javascript contextmenu dojo dojox.grid


【解决方案1】:

这是一个可能的解决方案(可能不是最好的),用于在 dojo 网格上使用上下文菜单进行选择:

视觉部分(声明性)

<table id="grid" dojoType="dojox.grid.EnhancedGrid"
  plugins="{indirectSelection:true,menus:{cellMenu:'GridCellMenu'}}">
  <div dojoType="dijit.Menu" id="GridCellMenu" style="display:none;">
    <div dojoType="dijit.MenuItem" id="mi_selectSimilar">select similar items</div>
    <div dojoType="dijit.MenuItem" id="mi_deSelectSimilar">DEselect similar items</div>
  </div>
  <thead>
    <tr>
      <th field="id">ID</th>
      <th field="lastname">Lastname</th>
      <th field="firstname>firstname</th>
    </tr>
  </thead>
</table>

JavaScript 背景

// Stylesheets and Dojo Groundwork are neglected in this example

<script type="text/javascript">
  dojo.require('dijit.Menu');
  dojo.require('dijit.MenuItem');
  dojo.require('dojox.grid.EnhancedGrid');
  dojo.require('dojox.grid.enhanced.plugins.IndirectSelection');
  dojo.require('dojox.grid.enhanced.plugins.Menu');

  var currentEvent = null;

  var fn_selectSimilar = function(){
    var data = currentCell.grid.store.objectStore.data;
    dojo.forEach(data,function(row,idx){
      if(row[currentEvent.cell.field] == data[currentEvent.rowIndex][currentEvent.cell.field]){
        currentEvent.cell.grid.selection.addToSelection(idx);
      }
    }
  }
  var fn_deSelectSimilar = function(){
    var data = currentEvent.cell.grid.store.objectStore.data;
    dojo.forEach(data,function(row,idx){
      if(row[currentEvent.cell.field] == data[currentEvent.rowIndex][currentEvent.cell.field]){
        currentEvent.cell.grid.selection.deselect(idx);
      }
    }
  }

  dojo.addOnLoad(function(){
    dojo.connect(dijit.byId('grid'),"onCellContextMenu",function(e){
      currentEvent = e;
    });
    dojo.connect(dijit.byId('mi_selectSimilar'),"onClick",fn_selectSimilar);
    dojo.connect(dijit.byId('mi_deSelectSimilar'),"onClick",fn_deSelectSimilar);
  });

</script>

【讨论】:

    【解决方案2】:

    这将遍历网格中所有选定的项目,并获取名为“YourGridColumnName”的单元格的值。

    var items = YourDataGridId.selection.getSelected();
    if (items.length) {
        dojo.forEach(items, function(selectedItem) {
            alert(YourDataGridId.store.getValues(selectedItem, "YourGridColumnName"));
        })
    }
    

    希望对你有帮助。

    【讨论】:

    • 遗憾的是,列名是我无法检索的内容之一。此外,未选择右键单击的单元格(用于上下文菜单)。我的目标是从右键单击的单元格中检索值,并根据上下文菜单中的用户选择选择具有相似条目的行。
    • 我会使用 dojox.grid.DataGrid 并使用 onRowContextMenu 事件而不是增强的数据网格。
    • 这个提示将我带到了 EnhancedGrid 的 onCellContextMenu。这可能是我迄今为止错过的拼图。
    【解决方案3】:

    您可以将事件处理程序链接到将弹出上下文菜单的鼠标和键盘事件。该事件有一个行索引,您可以将其存储在菜单项可以找到它的位置。

    【讨论】:

    • 我现在只了解 dojo 事件处理的基础知识。如何链接到现有的事件连接,或者您的意思是独立于现有的“菜单打开事件”的并行事件连接?您将如何存储该值以允许菜单项访问它 - 我可能只使用全局变量,这对我来说似乎不是很优雅。
    • 这个答案最接近我最终实施的解决方案,尽管 drcelus 和他的评论给了我最终的想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    相关资源
    最近更新 更多