The way I accomplish this is with a AbstractCell built from a
SafeHtmlTemplate that sinks the click event.
Have a template the feeds into the cell, the code will resemble something
like:
/**
* The HTML templates used to render the cell.
*/
public interface Templates extends SafeHtmlTemplates {
@SafeHtmlTemplates.Template("" +
"<div>" +
"<img src='IMAGE'/>" +
"<a id='YOURID'>{0}</a>" +
"<a id='YOURID2'>{1}</a>" +
"<span>TEXT</span>" +
"</div>")
SafeHtml cell(String linkText, String secondLinkText);
}
/**
* A custom {@link Cell} used to render a widget that displays the
search results
*/
private class TemplateCell extends AbstractCell< YOUROBJECT > {
/**
* Create a singleton instance of the templates used to render the
cell.
*/
private Templates templates = GWT.create(Templates.class);
public TemplateCell() {
/**
* Sink the click and keydown events. We handle click events in
this
* class. AbstractCell will handle the keydown event and call
* onEnterKeyDown() if the user presses the enter key while the
cell is
* selected.
*/
super("click", "keydown");
}
/**
* Called when an event occurs in a rendered instance of this Cell.
The
* parent element refers to the element that contains the rendered
cell, NOT
* to the outermost element that the Cell rendered.
*/
@Override
public void onBrowserEvent(Context context, Element
parent, YOUROBJECT value, NativeEvent event,
ValueUpdater< YOUROBJECT > valueUpdater)
{
// Let AbstractCell handle the keydown event.
super.onBrowserEvent(context, parent, value, event,
valueUpdater);
// Handle the click event.
if ("click".equals(event.getType())) {
// Ignore clicks that occur outside of the outermost
element.
EventTarget eventTarget = event.getEventTarget();
Element eventElement = Element.as(eventTarget);
if ("YOURID".equals(eventElement.getId())) {
handleTheClick(); // handle the click here
} else if ("YOURID2".equals(eventElement.getId())) {
handleTheOtherClick(); // handle the other click, if
you had two links
}
}
}
@Override
public void render(Context context, YOUROBJECT value,
SafeHtmlBuilder sb) {
/**
* Always do a null check on the value. Cell widgets can pass
null to
* cells if the underlying data contains a null, or if the data
arrives
* out of order.
*/
if (value == null) {
return;
}
// Use the template to create the Cell's html.
SafeHtml rendered = templates.cell(YOUROBJECT.firstText(),
YOUROBJECT.secondText());
sb.append(rendered);
}
}
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.