[英]GWT : Render a hyperlink in a TextColumn of a CellTable
首先-我是Java和GWT的初学者。 我有脚本语言背景,因此请明确。
我有一个CellTable,其中填充了来自数据库的数据(ServerKeyWord类获取数据)。
myCellTable.addColumn(new TextColumn<ServerKeyWord>() {
@Override
public String getValue(ServerKeyWord object) {
// TODO Auto-generated method stub
return object.getName();
}
});
上面的示例有效,但仅将数据显示为文本。 我需要使其成为一个超链接,当您单击它时,它会打开一个指向该位置的新标签。 我已经上网冲浪,得出的结论是我需要重写渲染。
public class HyperTextCell extends AbstractCell<ServerKeyWord> {
interface Template extends SafeHtmlTemplates {
@Template("<a target=\"_blank\" href=\"{0}\">{1}</a>")
SafeHtml hyperText(SafeUri link, String text);
}
private static Template template;
public static final int LINK_INDEX = 0, URL_INDEX = 1;
/**
* Construct a new linkCell.
*/
public HyperTextCell() {
if (template == null) {
template = GWT.create(Template.class);
}
}
@Override
public void render(Context context, ServerKeyWord value, SafeHtmlBuilder sb) {
if (value != null) {
// The template will sanitize the URI.
sb.append(template.hyperText(UriUtils.fromString(value.getName()), value.getName()));
}
}
}
现在...如何像第一个代码示例一样将HyperTextCell类与addColumn方法一起使用?
先感谢您!
HyperTextCell hyperTextCell = new HyperTextCell();
Column<ServerKeyWord, ServerKeyWord> hyperColumn = new Column<ServerKeyWord, ServerKeyWord>(
hyperTextCell) {
@Override
public ServerKeyWord getValue(ServerKeyWord keyWord) {
return keyWord;
}
};
myCellTable.addColumn(hyperColumn);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.