TreeDataGrid item selection and pre-positionning

Hi,

I’m trying to open a browse screen with TreeDataGrid from a LookupPickerField. I want to select a specific item (leaf in a hierarchy), expand ancestors and scroll to it.

I manage to select the item like this:

	@Subscribe
	protected void onAfterShow(final AfterShowEvent event) {

		if (null != this.hierarchieClient) {

			this.hierarchieClientsTable.setSelected(this.hierarchieClient);
			this.hierarchieClientsTable.scrollTo(this.hierarchieClientsTable.getSingleSelected(), DataGrid.ScrollDestination.MIDDLE);
		}
	}

but .scrollTo() throw an exception (row outside of datacontainer size). Indeed, selected row is position 388 and first level ancestors group size is 160

Best regards, Stef.

Hi,

In order to expand a certain branch down to the item, you need to expand all item parents, e.g.:

@Inject
private CollectionContainer<Group> groupsDc;
@Inject
private TreeDataGrid<Group> dataGrid;

@Subscribe
public void onAfterShow(AfterShowEvent event) {
    Group item = <required_item>;

    Group parent = item.getParent();
    List<Group> itemsToExpand = new ArrayList<>();
    while (parent != null) {
        itemsToExpand.add(parent);
        parent = parent.getParent();
    }

    dataGrid.expand(itemsToExpand);
    dataGrid.setSelected(item);
}

Regarding the scrollTo method. It’s a bug and I’ve created a GitHub issue.

Regards,
Gleb

1 Like

I see that issue is fixed in 7.1, thank you.