GWT - 如何让CellTable中的单元格使用HTML?

7
我有一个CellTable,我想在单元格中放置HTML代码。以下代码无法正常工作,输出中的空格被删除了。
    TextColumn<MyCell> column1 = new TextColumn<MyCell>()
    {
        @Override
        public String getValue(MyCell myCell)
        {
            String result = "     " +myCell.getValue();
            return result;
        }
    };
    table.addColumn(column1 , "Header1");

我知道这可以使用CSS完成,但我只想知道如何将HTML代码放入单元格中。任何帮助都将不胜感激!

1个回答

16

据我所知,在 HTML 中额外的空格会被忽略 - 你应该使用 pre 标签来保持格式。不管怎样,请看下面我的列示例。它可以从由数据提供者支持的对象中包含的值生成漂亮的进度条。

final SafeHtmlCell progressCell = new SafeHtmlCell();

    Column<UiScheduledTask, SafeHtml> progressCol = new Column<UiScheduledTask, SafeHtml>(
            progressCell) {

        @Override
        public SafeHtml getValue(UiScheduledTask value) {
            SafeHtmlBuilder sb = new SafeHtmlBuilder();
            float percent = new Float(value.getCompleted())
                    / new Float(value.getAll());
            int rounded = Math.round(percent * 100);
            sb.appendHtmlConstant("<div style='width: 100px; height: 20px; position: relative;'>");
            sb.appendHtmlConstant("<div style='z-index: 2; display: inline; width: 100px; position: absolute; left: 0px, top: 0px; text-align: center;'>"
                    + value.getCompleted()
                    + "/"
                    + value.getAll()
                    + "</div>");
            sb.appendHtmlConstant("<div style='position: absolute; left: 0; top: 0; width: 100px; z-index: 1'><div style='display: inline; float: left; width: "
                    + rounded
                    + "%; height: 20px; background-color: #82cd80;'></div>");
            sb.appendHtmlConstant("<div style='display: inline; float: right; width: "
                    + (100 - rounded)
                    + "%; height: 20px; background-color: #c54c4d;'></div></div>");
            sb.appendHtmlConstant("</div>");
            return sb.toSafeHtml();
        }
    };

感谢jgrabowski!这个完美地运行了!是的,除非你使用pre标签,否则HTML中的额外空格会被忽略。 - gusjap

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接