How to obtain the records of a group in a Group Table?

Hello everyone, I have a Group Table and I would do:

  1. I have a button that when pressed I check if I have selected a record of the table.
  2. If selected I would like to get through the group to which it belongs
  3. 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.

Hi Jose,

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.

Yuriy, excellent response.
Thanks and regards.

Hi.

Let me announce, that API of GroupTable and GroupDatasource is changed in the platform version 6.3.0.