0

my web application is based on dojo 1.6.0. The problem that I have is based on event handlers basically and/or their utilization in dojos "dojox.grid.EnhancedGrid" library.

My application contains a dojox Enhanced Grid with a great number of rows. (100+)

This Enhanced Grid makes use of the "cellMenu"-Plugin to show a context menu for each Grid Cell on right click.

My goal is to use the context menu for "smart" selection of rows.

For example:

The user right clicks on a cell wich is positioned in the column "lastname" and has the value "miller". He then clicks "smart select" in the context menu. The application will then loop over the row data and select all rows wich have "miller" as "lastname". In the aftermath the user will innitiate actions with the selected rows through press of a button.

Here is a small sourcecode example illustrating the declarative approach for visualization of the Enhanced Grid with Context Menu:

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

Action code is handled separate from the visualization in 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>

I am relatively new to dojo and do not have experience with the handling of the EnhancedGrid.

So my problem is the following:

When I click inside the context-menu which is a "dijit.Menu" the "onClick" event of the "dijit.MenuItem" contained therein is triggered.

Inside this event handler I need to read the contents of the "Grid Cell" the context menu was opened on, but I do not have (or do not currently know) a way to get a reference to the "Grid Cell".

With default tactics I might be able to get a reference to the MenuItem and from there maybe to the Menu, but I was unable to find an attribute containing the reference to the "Grid Cell" or a row/column ID that would enable me to access the Cell clicked.

Since context menus are there to do something with the "item" they were opened with by right clicking I think that there has to be a way (as meant by the designer) to access this "item".

I have not yet found a documentation or example illustrating this and would appreciate all your help.

1
  • I think that it's odd that the menuitem click event of a context-menu has no build-in reference to the clicked element. What is the point of a context menu - especially the cell context menu of the enhanced grid - if it cannot relate to the cell?
    – elfwyn
    Commented Aug 26, 2011 at 16:12

3 Answers 3

1

Here is a possible sollution (maybe not the best there is) for using a context menu on a dojo grid for selection purposes:

Visual Part (Declarative)

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

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

This will go through all the selected items in the grid and get the value of the cell named "YourGridColumnName".

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

Hope it helps.

3
  • Sadly the Column name is one of those things I am unable to retrieve. Also the Cell that is right-clicked (for a context menu) is not selected. My goal here is to retrieve the value from the right-clicked cell and select rows that have simmilar entries based on the users selection inside the context menu.
    – elfwyn
    Commented Aug 26, 2011 at 12:44
  • I would go with the dojox.grid.DataGrid and use the onRowContextMenu event instead of the enhanced data grid.
    – drcelus
    Commented Aug 31, 2011 at 9:14
  • This hint lead me to the onCellContextMenu of the EnhancedGrid. This could be the puzzle stone that I missed up until now.
    – elfwyn
    Commented Aug 31, 2011 at 13:51
0
+50

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.

2
  • Im only into the basics of dojo event handling right now. How do I link "into" an existing event connection, or did you mean a parallel event connection independent from the existing "menu-open-event"? How would You store the value to allow the menu item access to it - I would probaply just use a global var, wich doesn't seem very elegant to me.
    – elfwyn
    Commented Aug 31, 2011 at 7:51
  • This answer came closest to the sollution I implemented in the end, even though drcelus and his comment gave me the final idea.
    – elfwyn
    Commented Sep 5, 2011 at 9:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.