Reputation: 1526
In gwt cell table column,i created a column of type SafeHtmlCell.Inside i overridden the getvalue()
and onBrowserEvent()
Method. But when i click the column its not firing the event.See the below
@Override
public SafeHtml getValue(final Object object) {
SafeHtmlBuilder sb = new SafeHtmlBuilder();
sb.appendHtmlConstant("<B>");
sb.appendHtmlConstant(value);
sb.appendHtmlConstant("</B>");
sb.appendHtmlConstant("<i class='icon-pencil'></i></span>");
return sb.toSafeHtml();
}
@Override
public void onBrowserEvent(Context context, Element elem,
Object object, NativeEvent event) {
if ("click".equals(event.getType())) {
EventTarget eventTarget = event.getEventTarget();
if (elem.isOrHasChild(Element.as(eventTarget))) {
Element el = Element.as(eventTarget);
if ("icon-pencil".equals(el.getClassName())) {
Window.alert("Successfully clicked");
}
}
}
}
How to solve this? Wether i need override render()
Method as well?
Upvotes: 1
Views: 234
Reputation: 1526
SafeHtmlCell
don't have click event so i created my own custom class which extends AbstractCell<SafeHtml>
and overridden onBrowserEvent()
public class CustomSafeHtmlCell extends AbstractCell<SafeHtml>{
public CustomSafeHtmlCell(){
super(BrowserEvents.CLICK);
}
@Override
public void render(com.google.gwt.cell.client.Cell.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)) {
//logic...
}
}
}
and Used this CustomSafeHtmlCell instead of directly using SafeHtmlCell.
Upvotes: 0
Reputation: 511
When you create a custom cell, you need to override the render
method (not the getValue
method) and put your HTML inside the SafeHtmlBuilder sb
parameter.
Then if you want to listen for event, you need to override onBrowserEvent
(as you have done), but you also need to specify the consumedEvents
in the constructor of you class.
Example for your case:
public class TestCell extends AbstractCell<String> {
public TestCell() {
super("click");
}
@Override
public void onBrowserEvent(Context context, Element parent, String value,
NativeEvent event, ValueUpdater<String> valueUpdater) {
if ("click".equals(event.getType())) {
Window.alert("clicked");
}
}
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<B>");
sb.appendHtmlConstant(value);
sb.appendHtmlConstant("</B>");
sb.appendHtmlConstant("<i class='icon-pencil'></i></span>");
}
}
Upvotes: 1