Adding clickHandler to row in CellTable in GWT?
Asked Answered
G

2

14

I have created a basic CellTable and filled it with some data. Now I want to add a clickHandler to each row but I'm not sure how to do this. I've created a clickEvent for the whole table but I want one for each row.

    table.sinkEvents(Event.ONCLICK);
    table.setTitle("Click me");
    table.setSize("600px", "600px");
    table.addDomHandler(new ClickHandler()
    {
        @Override
        public void onClick(ClickEvent event)
        {
            Window.alert("You clicked!" +);

        }
    }, ClickEvent.getType());

Can I do something similar to add clickEvent for each row?

Greff answered 26/4, 2011 at 14:42 Comment(0)
R
30

A CellTable has built in support for handling click events. You can add a CellPreviewHandler that will be called among others when a row is clicked. It will receive a number of items in the event like the native event, cell, and data row value. Because it fires not only for click events you need to check if the click event was fired. Simply test the event passed: boolean isClick = "click".equals(event.getNativeEvent().getType()).

Another option is to extend the protected method doSelection, but it's deprecated and in you need to als make sure you have the right KeyboardSelectionPolicy set to make sure it's called when a click is done. See for the latter in the JavaDoc of the interface KeyboardSelectionPolicy.

Rhyolite answered 26/4, 2011 at 16:24 Comment(4)
I think you should consider using BrowserEvents.CLICK instead of "click".Wearproof
BrowserEvents is only available since GWT 2.5.0Rhyolite
@HilbrandBouwkamp: Is there any solution for doing the same if we use DataGrid instead of CellTable.Cotswolds
addCellPreviewHandler is part of AbstractHasData which is a parent class of both DataGrid and CellTable so it should work the same.Rhyolite
M
18

Another way to have a cell selected can be made using a NoSelectionModel and add it to the table:

//EDIT: this is a field, not a local variable
TheCellObject clickedObject; //the object selected by selectionModel

final NoSelectionModel<TheCellObject> selectionModel = new NoSelectionModel<TheCellObject>();

    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

                @Override
                public void onSelectionChange(SelectionChangeEvent event) {
                    clickedObject = selectionModel.getLastSelectedObject();
                }
            });
cellTable.setSelectionModel(selectionModel); //add selection model to your celltable
Moonlighting answered 30/5, 2011 at 14:25 Comment(2)
how can you access non final reference clickedObject inside of an inner class? How can you mutate it? your code seems incorrect.Rillet
This was a nice simple way for me to bypass the unwanted (for my project) extra styling that SingleSelectionModel adds, yet still be able to get the selected row in a click event in an older version of gwt. Thanks!Steam

© 2022 - 2024 — McMap. All rights reserved.