java - GWT : Render a hyperlink in a TextColumn of a CellTable

标签 java gwt hyperlink gwt-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);

关于java - GWT : Render a hyperlink in a TextColumn of a CellTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24140513/

相关文章:

java - 代码可以在 Netbeans 中运行,但不能在 TextPad 中运行?

java - ReSTLet 示例项目出错

html - 加密 CSS 外部链接并限制访问

eclipse - 如何将 Eclipse 中的 ctrl 单击功能映射为鼠标中键单击?

java - tomcat重启后数据库未连接,需要重新加载项目

java - 在单个 Java 类的命令行中使用 JUnit 编译和运行测试

java - 我可以使用 File.createTempFile() 创建具有非随机名称的文件吗

java - 从apache tomcat API调用获取时间

arrays - GWT:创建关联的 jsarray

ios - 如果url包含中文,如何通过swift获取链接