[此feature尚未在Vaadin 14框架中,但它实际上已经实现,即将进入下一个次要版本,即14.2]
同时有可能拥有a workaround using Template renderer,就像我在FileSelect组件中所做的一样。
根据Tatu Lund的回答,我们制作了自己的版本,并通过Vaadin 14.1.23进行了测试
/**
* {@code IconTreeGrid} adds a convenience method for a `TreeGrid` with node icons.
*
* Based on these answers:
*
* + https://vaadin.com/forum/thread/17625675/treegrid-displaying-icon-for-hierachy-column
* + https://stackoverflow.com/a/61268891/868941
*/
static class IconTreeGrid<T> extends TreeGrid<T> {
IconTreeGrid(HierarchicalDataProvider<T, ?> dataProvider) {
super(dataProvider);
}
public Column<T> addHierarchyColumn(Icon icon, ValueProvider<T, ?> valueProvider) {
final Column<T> column = addColumn(TemplateRenderer.<T>of(
"<vaadin-grid-tree-toggle leaf='[[item.leaf]]' expanded='{{expanded}}' level='[[level]]'>" +
"<iron-icon icon='[[item.icon]]' style='margin-right: 0.2em; [[item.style]]'></iron-icon>[[item.name]]" +
"</vaadin-grid-tree-toggle>")
.withProperty("leaf", item -> !getDataCommunicator().hasChildren(item))
.withProperty("icon", item -> icon.getElement().getAttribute("icon"))
.withProperty("style", item -> icon.getElement().getAttribute("style"))
.withProperty("name", item -> String.valueOf(valueProvider.apply(item))));
final SerializableComparator<T> comparator = (a, b) ->
compareMaybeComparables(valueProvider.apply(a), valueProvider.apply(b));
column.setComparator(comparator);
return column;
}
}
像魅力一样工作。