2011-08-25 47 views
0

我的web应用程序基于dojo 1.6.0。 我有的问题是基于事件处理程序基本上和/或他们在dojos“dojox.grid.EnhancedGrid”库中使用 。如何从单元格上下文菜单访问(增强)网格单元格数据?

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

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

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

例如:

用户右点击一个细胞至极被定位在列“姓氏”和具有值“米勒”。然后,他在上下文菜单中单击“智能选择”。 然后,应用程序将遍历行数据并选择“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-文件:

<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”的“点击”所载触发“dijit.MenuItem”的事件 内。

在这个事件处理程序中,我需要读取“Grid Cell”的内容,上下文菜单 已打开,但我没有(或不知道)一种方法来获取对“Grid细胞”。

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

由于上下文菜单是通过“右键单击”打开的“项目”,我认为必须有一种方法(如设计者所说)来访问此“项目”。

我还没有找到说明这一点的文档或例子,并希望得到您的帮助。

+0

我认为奇怪的是,上下文菜单的menuitem click事件没有内置引用单击的元素。上下文菜单的重点是什么 - 特别是增强型网格的单元格上下文菜单 - 如果它与单元格无关? – elfwyn

回答

1

这里是一个可能的sollution(也许不是最好有)使用在道场网格选择目的的上下文菜单:

视觉部分(声明)

<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> 
0

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

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

希望它有帮助。

+0

不幸的是,列名是我无法检索的那些东西之一。另外,右键单击的单元格(对于上下文菜单)未被选中。我的目标是从右键单元格中检索值,并根据上下文菜单中的用户选择选择具有相似条目的行。 – elfwyn

+0

我会使用_dojox.grid.DataGrid_并使用_onRowContextMenu_事件而不是增强的数据网格。 – drcelus

+0

此提示将我引向EnhancedGrid的onCellContextMenu。这可能是我迄今为止遗漏的难题。 – elfwyn

0

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

+0

现在我只进入dojo事件处理的基础知识。如何将“连接”到现有的事件连接中,或者是否指独立于现有“菜单打开事件”的并行事件连接?你将如何存储值以允许菜单项访问它 - 我可能只是使用全局变量,这对我来说看起来并不是很优雅。 – elfwyn

+0

尽管drcelus和他的评论给了我最后的想法,但这个答案最接近我最后实施的溶剂。 – elfwyn