在 Vaadin Grid 中显示超链接

问题描述 投票:0回答:1

我正在尝试在 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 vaadin-grid
1个回答
0
投票

答案就在使用 Vaadin LitRenderer 的动态 HTML

我选择使用

ComponentRenderer
来渲染不安全的 HTML:

grid.addColumn(new ComponentRenderer<>(info -> new Html(String.format("<span>%s</span>", info.getValue())))).setTextAlign(ColumnTextAlign.START);
© www.soinside.com 2019 - 2024. All rights reserved.