javaxiss
javaxiss

Reputation: 734

How to add a Clickhandler to a cellTable cell (or row )

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.

Upvotes: 11

Views: 12301

Answers (3)

Abdul Rouf Abu
Abdul Rouf Abu

Reputation: 1

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())

Upvotes: 0

jscott
jscott

Reputation: 1051

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.

Upvotes: 17

z00bs
z00bs

Reputation: 7498

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

Upvotes: 2

Related Questions