在 Vaadin Flow / 14 TreeGrid 组件中使用节点图标

Using node icons in Vaadin Flow / 14 TreeGrid component

示例(VueJs / Vuetify)我想实现:

Vaadin 只有 TreeGrid#addHierarchyColumn(ValueProvider) 不允许添加图标。

这个feature还没有在Vaadin 14框架中,但是它刚刚被实际实现并且即将进入下一个小版本,即14.2

同时可以有 a workaround using Template renderer, like I have done it in my 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
 * + 
 */
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;
    }

}

很有魅力。