Hi,
I am trying to add an icon with text or only an icon in the hierarchyColumn in the TreeDataGrid. I can understand that i have to use the custom renderer or using the partNameGenerator. So, Please help me with sample code.
I understand your confuse, if your create generator for column, hierarchy from datagrid disappears, thats uncommon behaviour. So, i did following steps to add “generatable” column that can use as hierarchy column:
Remark: i use kotlin, so maybe i take mistakes or typos inside java code.
I used UserListView as base view for my experiments
<treeDataGrid id="usersDataGrid"
width="100%"
minHeight="20em"
dataContainer="usersDc"
hierarchyProperty="dependsOn"> thats my hierarchy property
<actions>
... same
</actions>
<columns resizable="true">
... same
</columns>
</treeDataGrid>
We need to subscribe BeforeShowEvent, inside it remove our first column “username”, generate new column that would be not a column, but HierarchyColumn
@ViewComponent
private TreeDataGrid<User> usersDataGrid;
@Subscribe
public void onBeforeShow(BeforeShowEvent event) {
var columns = usersDataGrid.getColumns();
columns.forEach(it -> usersDataGrid.removeColumns(it));
var addComponentHierarchyColumn = usersDataGrid.addComponentHierarchyColumn((user) -> {
var horizontalLayout = new HorizontalLayout();
// here you can do anything to provide information about user; use this horizontalLayout to place your components
horizontalLayout.add(new Avatar());
horizontalLayout.add(new Text(user.username));
return horizontalLayout;
});
addComponentHierarchyColumn.setHeader("Username");
}
getPropertyValueByNameReflection method is omitted because i use Kotlin; you need to impement this method on you own
@ViewComponent
private TreeDataGrid<User> usersDataGrid;
@Subscribe
public void onBeforeShow(BeforeShowEvent event) {
var columns = usersDataGrid.getColumns()
columns.forEach(it -> usersDataGrid.removeColumns(it));
var addComponentHierarchyColumn = usersDataGrid.addComponentHierarchyColumn((user) -> {
var horizontalLayout = new HorizontalLayout();
// here you can do anything to provide information about user; use this horizontalLayout to place your components
horizontalLayout.add(new Avatar());
horizontalLayout.add(new Text(user.username));
return horizontalLayout;
});
addComponentHierarchyColumn.setHeader("Username");
Lis.of("firstName", "lastName", "email", "active", "timeZoneId").forEach(propName -> {
usersDataGrid.addComponentColumn({ user -> {
String propertyValue = getPropertyValueByNameReflection(user, propName);
return new Text(propertyValue);
})
.setHeader(propName);
}