我正在尝试在 Vaadin 网格列中显示 HTML 链接。
我有以下代码:
Grid<DetailInfoRow> grid = new Grid<>();
grid.addColumn(LitRenderer.<DetailInfoRow> of("<span>${item.name}</span>").withProperty("name", DetailInfoRow::getName)).setTextAlign(ColumnTextAlign.START);
grid.addColumn(LitRenderer.<DetailInfoRow> of("<span>${item.value}</span>").withProperty("value", DetailInfoRow::getValue)).setTextAlign(ColumnTextAlign.START);
grid.setSelectionMode(Grid.SelectionMode.NONE);
grid.setAllRowsVisible(true);
value
的DetailInfoRow
字段是String
类型,有时具有类似于"<a href="http://10.1.194.60:8888/update">10.1.194.60</a>"
的值。
但在浏览器中,它被呈现为文字,而不是解释为外部资源的链接。我认为这就是 LitRenderer
可以做到的。
请问上面的代码有什么问题吗?谢谢你。
答案就在使用 Vaadin LitRenderer 的动态 HTML。
我选择使用
ComponentRenderer
来渲染不安全的 HTML:
grid.addColumn(new ComponentRenderer<>(info -> new Html(String.format("<span>%s</span>", info.getValue())))).setTextAlign(ColumnTextAlign.START);