Tim
Tim

Reputation: 4284

GWT: React to a click on a SafeHTML in a cell of a CellTable?

I am using a CellTable to display results of a query and I need these results to be shown as (html) links. I would like to react to a click on these links. So far, I had implemented it like this:

    // ClickHandler on CellTable
cellTable.addCellPreviewHandler(new Handler<OperationalItemMultipleSearchRowData>() {
    @Override
    public void onCellPreview(CellPreviewEvent<OperationalItemMultipleSearchRowData> event) {
        boolean isClick = "click".equals(event.getNativeEvent().getType());
        if (isClick) 
            AdminUtils.EVENT_BUS.fireEvent(new SimpleSearchEvent(event.getValue().getName()));              
    }
});

The Problem is that this reacted to a Click on the whole row instead of the link. Due to architectural restrictions, the link itself is not a real html link, but a SafeHtml link that leads nowhere. I just needed the look&feel of a Link:

Column<OperationalItemMultipleSearchRowData, SafeHtml> nameColumn = new Column<OperationalItemMultipleSearchRowData, SafeHtml>(new SafeHtmlCell()) {

            @Override
            public SafeHtml getValue(final OperationalItemMultipleSearchRowData object) {
                return new SafeHtml() {

                    @Override
                    public String asString() {
                        return "<a href=\"javascript:;\">" + object.getName() + "</a>";
                    }               
                };
            }
        };

How can I react to a click on this link only ? (instead of the whoel row) Is there a more elegant way to implement this ?

Cheers

Upvotes: 0

Views: 684

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64561

As with any other use of event delegation, the basic idea is to find walk up the hierarchy starting from the target of the event up until you find the link element you're looking for, or some other element that signals the search is over and the click was targetted outside the link (e.g. you reached the cell, the row or the table).

That being said, I think you should merge your behavior inside a specific Cell implementation rather than using a CellPreviewHandler (copy/paste the ActionCell or TextButtonCell as a starting point).

As a side note, I also believe you should not use a link when you're not actually linking anywhere, or try to provide a target for the link if the behavior is the one of a link (that way, right-clicking, middle-clicking or ctrl-clicking will Just Work™). If you want the look of a link (without the "feel"), then just use an ActionCell or TextButtonCell and style it accordingly.

Upvotes: 1

Related Questions