PDA

View Full Version : Which grid row is clicked when opening context menu?



sbarkdull
26 Nov 2008, 8:32 AM
I am trying to figure out which row in the Grid the user right-clicked in when opening the context menu in a grid.

I am adding a SelectionListener to a MenuItem, which is added to a Menu, which is set as the Grid's context menu (see code snippet below). Do I need to do something like detect a right click in the grid, save which row was clicked in, and make that row number available to the componentSelected() method in the listener? Is there a way to get the row index information from the MenuEvent in the listener?

Menu m = new Menu();
Item item = new MenuItem( "Respond", "reply-menu-item", new SelectionListener<MenuEvent>() {

@Override
public void componentSelected(MenuEvent ce) {
int rowIndex = do something here to get the row index
handleReplyRequest( rowIndex );
}

});
m.add( item );
grid.setContextMenu( m );

knockatthedoor
15 Dec 2008, 10:55 AM
Sorry, didnt see your post was in the GXT section im working on EXTJS

karacutey
15 Dec 2008, 5:35 PM
grid.getSelectionModel().getSelectedItem().get("dataModelPropertyName")

where grid is your grid component, you can access that via static reference or via the selection event

for the selection event it would be something like


public void handleEvent(SelectionEvent se) {
ModelData rowData = (ModelData) se.selection.get(0);
}

karacutey
15 Dec 2008, 9:17 PM
okay this is tested and works. only right clicks on the row.

this extends a content panel IE XMLGridPanel Class


package com.yourorg.yourproject

import com.allen_sauer.gwt.log.client.Log;
import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author Kara
*/
public class ViewCompanyConfsSchedTabPanel extends TabItem {

public ViewCompanyConfsSchedTabPanel(String id, String companyId, String name) {

//do some queries or something

XMLGridPanel gp = new XMLGridPanel();
gp.setRequestUrl(url);

ColumnConfig columnConfigNameFirst = new ColumnConfig("nameFirst", "First Name", 100);
ColumnConfig columnConfigNameLast = new ColumnConfig("nameLast", "Last Name", 100);

List<ColumnConfig> config = new ArrayList<ColumnConfig>();

config.add(columnConfigNameFirst);
config.add(columnConfigNameLast);

ModelType mt = new ModelType();
mt.root = "...";

//define the rest of your model here

final ColumnModel cm = new ColumnModel(config);
gp.setColumnModel(cm);
gp.setModelType(mt);

gp.show();
gp.layout();

//////////////////////////////////////////////////////////////////////////////////////////////////////////
//SETS YOUR CONTEXT MENU
//
gp.getGrid().setContextMenu(new ConfSchedContextMenu());
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////

this.setLayout(new FitLayout());

this.add(gp);
this.layout();

//////////////////////////////////////////////////////////////////////////////////////////////////////////
//SETS YOUR CONTEXT MENU LISTENERS
//
setGridListeners(gp);
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////

//Perform some load method or init on your Grid to Display load it
}

private void setGridListeners(XMLGridPanel xmlGridPanel) {
final Grid grid = (Grid) xmlGridPanel.getItemByItemId("xmlGridPanel");
grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

grid.addListener(Events.ContextMenu, new Listener<GridEvent>() {

public void handleEvent(GridEvent comeOn) {

String cI = comeOn.event.getTarget().getClassName();
String mS = "-hd-inner";

comeOn.doit = ifItIsAGridRow(comeOn);

Log.info("[CONTEXT CALL] " + comeOn.doit);
}
});
}

private boolean ifItIsAGridRow(GridEvent ge) {
String cI = ge.event.getTarget().getClassName();
String mS = "-hd-inner";

if (cI.indexOf(mS) == -1) {
return true;
} else {
return false;
}
}
}

ErikL
14 Jan 2009, 1:31 AM
Thanks Kara. Excellent post, helped me a lot when implementing context menu for Grid cells, i.e:



grid.addListener(Events.ContextMenu, new Listener<GridEvent>() {

public void handleEvent(GridEvent be) {

((CellSelectionModel<BaseModel>) be.grid.getSelectionModel())
.selectCell(be.rowIndex, be.colIndex);
}

});