How to add a link (anchor) to a CellTable?

1,468 views
Skip to first unread message

Magnus

unread,
Jan 28, 2012, 7:46:52 AM1/28/12
to google-we...@googlegroups.com
Hi,

can I add an anchor object to a cell table?

I just found "TextColumn" as the only reasonable subclass of Column:

TextColumn<Entry> col_myCol = new TextColumn<Entry>()

But I need a type where I can insert an anchor...

And hints?

Thanks
Magnus

kim young ill

unread,
Jan 28, 2012, 9:41:40 AM1/28/12
to google-we...@googlegroups.com
write your own cell & override method render

hth

Magnus

--
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/-/Qr-Yxkc3S88J.
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.

Magnus

unread,
Jan 28, 2012, 10:32:03 AM1/28/12
to google-we...@googlegroups.com
Thank you, but it is not clear yet:
Could you please mention the class that I should subclass?
And what should I use instead of TextCell?

Thanks!

  TextColumn<Entry> myCol = new TextColumn<Entry>()
  {
   @Override
   public String getValue(Entry obj)
   {
    return (obj.myAttribute);
   }
  };

Andrea Boscolo

unread,
Jan 28, 2012, 10:37:56 AM1/28/12
to google-we...@googlegroups.com

Jens

unread,
Jan 28, 2012, 10:53:13 AM1/28/12
to google-we...@googlegroups.com
You will use the Column class directly and tell it to use your custom cell:

Column col = new Column<Entry, String>(new YourCustomCell()) {
   @Override
   public String getValue(Entry obj)
   {
    return (obj.myAttribute);
   }
}

How to implement a custom cell can be found in the documentation.


If you only need a simple link you could also just use the SafeHtmlCell:

public interface SimpleCellTemplates extends SafeHtmlTemplates {
  @Template("<a href=\"{0}\">{1}</a>")
  SafeHtml anchor(SafeUri href, String name);
}

static final SimpleCellTemplates cell = GWT.create(SimpleCellTemplates.class)

Column col = new Column<Entry, SafeHtml>(new SafeHtmlCell()) {
   @Override
   public SafeHtml getValue(Entry obj)
   {
    SafeUri href = //construct your href as SafeUri using UriUtils
    return cell.anchor(href, obj.getName());
   }
}

kim young ill

unread,
Jan 28, 2012, 10:55:09 AM1/28/12
to google-we...@googlegroups.com
check out the gwt-showcase for some examples.

On Sat, Jan 28, 2012 at 4:37 PM, Andrea Boscolo <andr...@gmail.com> wrote:
See docs: http://code.google.com/webtoolkit/doc/latest/DevGuideUiCustomCells.html

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

Arash

unread,
Jun 11, 2012, 4:44:11 PM6/11/12
to google-we...@googlegroups.com
public class ClickableSafeHtmlCell extends AbstractCell<SafeHtml> {
/**
 * Construct a new ClickableSafeHtmlCell.
 */
public ClickableSafeHtmlCell() {
    super("click", "keydown");
}

@Override
public void onBrowserEvent(Context context, Element parent, SafeHtml value, NativeEvent event,
        ValueUpdater<SafeHtml> valueUpdater) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);
    if ("click".equals(event.getType())) {
        onEnterKeyDown(context, parent, value, event, valueUpdater);
    }
}

@Override
protected void onEnterKeyDown(Context context, Element parent, SafeHtml value,
        NativeEvent event, ValueUpdater<SafeHtml> valueUpdater) {
    if (valueUpdater != null) {
        valueUpdater.update(value);
    }
}

@Override
public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
    if (value != null) {
        sb.append(value);
    }
}

And then usage:

Column<YourProxy, SafeHtml> nameColumn = new Column<YourProxy, SafeHtml>(
        new ClickableSafeHtmlCell()) {
    @Override
    public SafeHtml getValue(YourProxy object) {
        SafeHtmlBuilder sb = new SafeHtmlBuilder();
        sb.appendHtmlConstant("<a>");
        sb.appendEscaped(object.getName());
        sb.appendHtmlConstant("</a>");
        return sb.toSafeHtml();
    }
};

nameColumn.setFieldUpdater(new FieldUpdater<YourProxy, SafeHtml>() {
        @Override
        public void update(int index, YourProxy object, SafeHtml value) {
             Window.alert("You have clicked: " + object.getName());

        }
    });
Reply all
Reply to author
Forward
0 new messages