首先 - 我是 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);
关于java - GWT : Render a hyperlink in a TextColumn of a CellTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24140513/