How to handle double click/enter key on the row of a datagrid ?

637 views
Skip to first unread message

Marius Grama

unread,
Oct 18, 2012, 8:01:33 AM10/18/12
to google-we...@googlegroups.com
I have a scenario where i am working with a GWT DataGrid on which when the user makes a double-click an event should be raised containing the information about the model bean of the selected selected row.
e.g : For a DataGrid containing informations about  persons (columns: first name , last name, age) when a double click (or enter key on the currently selected row of the datagrid) is made on one of the rows of the datagrid a MessageBox should appear containing the full name person selected.

I've tried for some time to implement this scenario, but I haven't found so far an elegant (and simple) way to implement it.

Can anybody assist me on this problem?

Marius Grama

unread,
Oct 18, 2012, 8:08:41 AM10/18/12
to google-we...@googlegroups.com
My (dirty) workaround was extending AbstractCell class and adding the custom code within:

@Override
public void onBrowserEvent(final Context context, final Element parent, final C value,
                final NativeEvent event, final ValueUpdater<C> valueUpdater) { 

                 HasData<T> data = this.column.getTable();
                SelectionModel<? super T> selectionModel = data.getSelectionModel();

                if (selectionModel instanceof SingleSelectionModel) {
                    T val = (T) ((SingleSelectionModel<? super T>) selectionModel).getSelectedObject();
                    userChoiceHandler.onUserChoice(val);
                }
}

method.

btw, has anyone an idea  why the method getSelectionModel() from HasData<T> class returns
SelectionModel<? super T>
instead of 
SelectionModel<T>
?

Jens

unread,
Oct 18, 2012, 8:27:29 AM10/18/12
to google-we...@googlegroups.com
If you want to do this outside the Datagrid you can use DataGrid.addCellPreviewHandler(). The event contains all the information you need.

And it is SelectionModel<? super T> because its less restrictive. For example you can have a DataGrid<Integer> that holds a more generic SelectionModel<Number>. Using generics that are least restrictive while ensuring type safety is a good practice for libraries/toolkits.

-- J.

Marius Grama

unread,
Oct 18, 2012, 9:01:35 AM10/18/12
to google-we...@googlegroups.com
Thanks Jens for the tip.

For anybody interested on the solution I've implemented it in the following manner :

        addCellPreviewHandler(new CellPreviewEvent.Handler<T>() {

            @Override
            public void onCellPreview(final CellPreviewEvent<T> event) {
                // userChoiceHandler is used to notify an item choice within the datagrid.
                if (userChoiceHandler != null) {
                    NativeEvent nativeEvent = event.getNativeEvent();
                    String eventType = nativeEvent.getType();
                    if ((BrowserEvents.KEYDOWN.equals(eventType) && nativeEvent.getKeyCode() == KeyCodes.KEY_ENTER)
                            || (BrowserEvents.DBLCLICK.equals(nativeEvent.getType()))) {
                        T selectedValue = event.getValue();
                        userChoiceHandler.onUserChoice(selectedValue);
                    }
                }
            }
        });
Reply all
Reply to author
Forward
0 new messages