0

我的 Web 应用程序基于 dojo 1.6.0。我遇到的问题基本上基于事件处理程序和/或它们在dojos“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 比较陌生,并且没有处理EnhancedGrid 的经验。

所以我的问题如下:

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

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

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

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

我还没有找到说明这一点的文档或示例,希望您能提供所有帮助。

4

3 回答 3

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>
于 2011-09-05T11:53:56.237 回答
0

You can link an event handler into the mouse and keyboard events that will bring up the context menu. The event has a row index that you can store in a place where the menu item will find it.

于 2011-08-31T07:38:33.063 回答
0

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

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

希望能帮助到你。

于 2011-08-26T09:51:12.680 回答