1
votes

I have a GWT DataGrid with a multi-selection model and check-boxes to show selection/select/deselect rows. That's all well and good.

But, I also want to have a second, independent selection model. If a user double-clicks on a row, I want to handle that event, and for the event handler to know which row was double-clicked. The double-clicking should not affect the check-box selection.

I tried this:

final SelectionModel<MyRecord> selectionModel = new MultiSelectionModel...
//Yes I need a MultiSelectionModel

dataGrid.addDomHandler(new DoubleClickHandler() {

  public void onDoubleClick(DoubleClickEvent event) {
    selectionModel.get??? //no suitable getter for double-clicked
  }

}, DoubleClickEvent.getType());

But ran into a dead-end when I found now way to get the double-clicked row in the event handler. One way would be to register both a Multi- and Single- selection model, but doubt DataGrid will support that.

Neither can I work out how to get the clicked row from the DoubleClickEvent object.

I have implemented a button cell with a FieldUpdater. This works, but it's not ideal.

Any suggestions?

2

2 Answers

2
votes

If I understand correctly you want to get the index of the row.

You could do it like this: (this way you'll get the "Real" index)

AbstractSelectionModel<T> selectionModel = (AbstractSelectionModel<T>)dataGrid.getSelectionModel();

ArrayList<Integer> intList = new ArrayList<Integer>();

List<Row> list = (List<Row>)_dataProvider.getList();

int i = 0;

for(Row row : list)
{
    if( selectionModel.isSelected(row) )
        intList.add(i);

    i++;
}

UPDATE:

To get only the current row you could do that:

datagrid.getKeyboardSelectedRow()
0
votes

I'm 3 years too late to the party, but I think the more correct solution would be:

    dataGrid.addCellPreviewHandler(new CellPreviewEvent.Handler<YOUR_MODEL_TYPE>() {

        @Override
        public void onCellPreview(final CellPreviewEvent<YOUR_MODEL_TYPE> event) {

            if (BrowserEvents.DBLCLICK.equalsIgnoreCase(event.getNativeEvent().getType())) {
                int row = event.getIndex();
                doStuff(row); // do whatever you need the row index for
            }
        }
    });