Reputation: 31
I defined a safehtmlcell on my datagrid. The html renders just fine but I need that cell to be clickable which works on a clickabletextcell. Here is my code:
SafeHtmlCell itemCell = new SafeHtmlCell();
Column<MyStoreItems, SafeHtml> itemNameColumn = new Column<MyStoreItems, SafeHtml>(itemCell) {
@Override
public SafeHtml getValue(MyStoreItems storeItem) {
SafeHtmlBuilder sb = new SafeHtmlBuilder();
sb.appendHtmlConstant("<table>");
sb.appendHtmlConstant("<td style='font-size:95%;'>");
sb.appendEscaped(storeItem.getItemName());
sb.appendHtmlConstant("</td></tr><tr><td>");
sb.appendEscaped(storeItem.getPackingUnit() + " " + storeItem.getStoreItemName());
sb.appendHtmlConstant("</td></tr></table>");
return sb.toSafeHtml();
}
};
itemNameColumn.setFieldUpdater(new FieldUpdater<MyStoreItems, SafeHtml>() {
@Override
public void update(int index, MyStoreItems storeItem, SafeHtml value) {
selectedStoreItem = storeItem;
selectedIndex = index;
showSelectedStoreItems(index, storeItem);
}
});
Is there any workaround to make the safehtml cell clickable?
Upvotes: 1
Views: 1127
Reputation: 3492
You have to create a class that extends AbstractCell, then override the onBrowserEvent()
method.
public class MySafeHtmlCell extends AbstractCell<SafeHtml> {
public MySafeHtmlCell() {
super(BrowserEvents.CLICK);
}
@Override
public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
if (value != null) {
sb.append(value);
}
}
@Override
public void onBrowserEvent(Context context, Element parent, SafeHtml value, NativeEvent event,
ValueUpdater<SafeHtml> valueUpdater) {
String eventType = event.getType();
if (BrowserEvents.CLICK.equals(eventType)) {
// Do something...
}
}
}
In the above implementation, I made the a copy of SafeHtmlCell implementation and added click events handling. Of course you should consider just extending SafeHtmlCell instead of rewriting it.
Unrelated note: instead of using a safeHtmlBuilder consider using HtmlTemplates
public interface MyTemplate extends SafeHtmlTemplates {
@Template("<span class=\"{3}\">{0}: <a href=\"{1}\">{2}</a></span>")
SafeHtml messageWithLink(SafeHtml message, String url, String linkText,
String style);
}
Upvotes: 1
Reputation: 3048
Just use a ClickableTextCell
. It extends AbstractSafeHtmlCell
and consumes the click as well as the keydown events.
Upvotes: 0