@Override public void render(Context context, ContactInfo value, SafeHtmlBuilder sb) { // Value can be null, so do a null check.. if (value == null) { return; } // Add the contact image. sb.appendHtmlConstant("<tr><td rowspan='3'>"); sb.appendHtmlConstant(imageHtml); sb.appendHtmlConstant("</td>");...
I want to make a column of clickable image inside a celltable, if user click the image, a new tab page is created.
the problem is how to make such a clickable image cell column?
package com.mycompany.project.client;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
// ClickableImageCell extends ClickableTextCell
/*
* http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/cell/client/AbstractCell.html#AbstractCell%28java.lang.String...%29
*/
public class ClickableImageCell2 extends AbstractCell<String>{
public ClickableImageCell2() {
/*
* Let the parent class know that our cell responds to click events and
* keydown events.
*/
super("click", "keydown");
}
public void onBrowserEvent(Context context, Element parent, String value,
NativeEvent event, ValueUpdater<String> valueUpdater) {
// Check that the value is not null.
if (value == null) {
return;
}
// Call the super handler, which handlers the enter key.
super.onBrowserEvent(context, parent, value, event, valueUpdater);
// On click, perform the same action that we perform on enter.
if ("click".equals(event.getType())) {
this.onEnterKeyDown(context, parent, value, event, valueUpdater);
}
}
/**
* By convention, cells that respond to user events should handle the enter
* key. This provides a consistent user experience when users use keyboard
* navigation in the widget.
*/
protected void onEnterKeyDown(Context context, Element parent,
String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
Window.alert("You clicked " + value);
}
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
// TODO Auto-generated method stub
if (value != null){
sb.appendHtmlConstant("<img src =\"" + value + "\" alt=\"image\" />");
}
}
}
AbstractCell(java.lang.String... consumedEvents)
Construct a newAbstractCell
with the specified consumed events.
ClickableImageCell2 clickableImageCell2 = new ClickableImageCell2();
Column<Contact, String> clickableImageColumn2 = new Column<Contact, String>(clickableImageCell2){
@Override
public String getValue(Contact object) {
// TODO Auto-generated method stub
return "/images/line.png";
}
};
public void onBrowserEvent(Context context, Element parent, String value,
NativeEvent event, ValueUpdater<String> valueUpdater)
public void onBrowserEvent(Context context, Element parent, C value,in AbstractCell,
NativeEvent event, ValueUpdater<C> valueUpdater)
@Override public void onBrowserEvent(Context context, Element parent, Contact value, NativeEvent event, ValueUpdater<Contact> valueUpdater)
I'm not really sure if this is the best implementation, but it works for me. -- First, add this to your constructor:
public HeaderCell() {
super("click", "keydown");
}
-- Then, override the onBrowserEvent:
@Override
public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
// Let AbstractCell handle the keydown event.
super.onBrowserEvent(context, parent, value, event, valueUpdater);
// Handle the click event.
if ("click".equals(event.getType())) {
EventTarget eventTarget = event.getEventTarget();
// in here we check whether the cell that was being clicked is an image, not the entire cell
if(eventTarget.toString().contains("img src") && !eventTarget.toString().contains("<div class")){
// do something if it's indeed the image that was clicked
}
}
}
Cheers, Lin
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/nImJY4gKNjcJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater)
for example,
if I create an instance of HeaderCell() headerCell1, and then I create another instance headerCell2, click on these two instances will have the same response hard code in the class HeaderCell().
// do something if it's indeed the image that was clicked
what should I do if I want click on each instance of HeaderCell() will have its custom logic?