Hello everyone, I have a Group Table and I would do:
I have a button that when pressed I check if I have selected a record of the table.
If selected I would like to get through the group to which it belongs
When I have the group I would like to get all the records in the table belonging to that group
How could I do ?, by the datasource or by Group Table?
Greetings and thanks.
For now API of GroupTable and GroupDatasource does not have a method to obtain groups of item, but you can iterate GroupDatasource groups and find what group contains the item.
Example:
// Helper method to iterate all nested groups
public static <T extends Entity> GroupInfo findGroup(GroupDatasource<? extends Entity, ?> groupDs,
List<GroupInfo> groups, T item) {
for (GroupInfo group : groups) {
List<GroupInfo> children = groupDs.getChildren(group);
GroupInfo groupInfo = findGroup(groupDs, children, item);
if (groupInfo != null) {
return groupInfo;
}
if (groupDs.getGroupItemIds(group).contains(item.getId())) {
return group;
}
}
return null;
}
public void buttonClick() {
User user = usersTable.getSingleSelected();
GroupInfo group = findGroup(usersDs, usersDs.rootGroups(), user);
if (group != null) {
// get all items of group
Collection<UUID> groupItemIds = usersDs.getGroupItemIds(group);
List<User> groupUsers = groupItemIds.stream()
.map(id -> usersDs.getItem(id))
.collect(Collectors.toList());
showNotification("Group size: " + groupUsers.size(), NotificationType.HUMANIZED);
}
}
And thank you for your question, we are planning to improve API of GroupDatasource in one of the next minor releases.