How to add a Clickhandler to a cellTable cell (or row )
Asked Answered
F

3

11

I would like to have a handler on a column of my cellTable.The column is an ImageResourceCell and I would that when I click on it, it delete the row Here is my code

Column<MyObject, ImageResource> imageColumn = 
    new Column<MyObject, ImageResource>(newImageResourceCell()) {
        @Override
        public ImageResource  getValue(MyObject object) {
             return Bundle.Util.getInstance().deleteRegexButton();
        }
    }; 
cellTable.addColumn(imageColumn,SafeHtmlUtils.fromSafeConstant("<br/>");

But I didn't know how to insert a handler as described Is it possible ??

any suggestions are welcome

Thanks.

Fitful answered 31/5, 2011 at 7:50 Comment(0)
P
17

Cells have to declare the events they handle, then the browser event can be passed to the cell.

    ImageResourceCell myImgCell = new ImageResourceCell() {
        public Set<String> getConsumedEvents() {
            HashSet<String> events = new HashSet<String>();
            events.add("click");
            return events;
        }
    };

    Column<MyObject, ImageResource> imageColumn = new Column<MyObject, ImageResource>(myImgCell) {
        @Override
        public ImageResource getValue(MyObject dataObj) {
                    return Bundle.Util.getInstance().deleteRegexButton();
        }

        @Override
        public void onBrowserEvent(Context context, Element elem,
                MyObject object, NativeEvent event) {
            super.onBrowserEvent(context, elem, object, event); 
            if ("click".equals(event.getType())) {
                //call your click event handler here
            }
        }
    };

More info here: http://code.google.com/webtoolkit/doc/latest/DevGuideUiCustomCells.html

Note: this works with GWT 2.4, did not try with GWT 2.2.

Preoccupied answered 27/12, 2011 at 17:12 Comment(0)
K
2

Have you seen Adding clickHandler to row in CellTable in GWT??

Keelson answered 31/5, 2011 at 7:57 Comment(1)
Yes and it didn't work the cellTable cannot have NoSelectionModel as a selectionModelFitful
M
0

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())
Marley answered 19/10, 2021 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.