eventsgwtgwt-celltableevent-propagation

stop click propagation of custom cell in CellTable


I've a CellTable to which I attach a click handler(via addDomHandler). Then I've added a custom cell which handles onBrowserEvent(...). I'd like to stop the event to propagate in the cell's onBrowserEvent so that the table handler is not invoked anymore. Is this possible?

table = new CellTable();
table.addDomHandler(new ClickHandler() {
                @Override
                public void onClick(final ClickEvent pEvent) {
                    Trace.info("this shouldn't trigger");
                }
            }, ClickEvent.getType());
table.addColumn(new IdentityColumn<MyVO>(new MyCell()));

class MyCell extends AbstractCell<MyVO> {
@Override
            public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context pContext, Element pParent,
                    Handle<DnSuggestionDetailsVO> pValue, NativeEvent pEvent,
                    ValueUpdater<Handle<DnSuggestionDetailsVO>> pValueUpdater) {
                Trace.info("cell onBrowserEvent handled, propagation should stop here!");
                pEvent.stopPropagation();
            }
}

Thank you!


Solution

  • It's easier to cancel an event before it reaches the cell:

    table.addCellPreviewHandler(new Handler<Item>() {
        @Override
        public void onCellPreview(CellPreviewEvent<Item> event) {
            //do something
            event.setCancelled(true);
        }
    });
    

    Note that CellPreviewHandler already monitors all events within a table. You can use it for your ClickEvent as well (with finer control like which column is clicked) instead of adding a ClickHandler to the entire table.