Ensure visible tree item

As you know some GUI APIs have possibility to make to ensure visible tree item:


tree.ensureVisible(itemId);

After this the itemId item becomes visible.

Do you plan implement this feature in the Vaadin tree component? May be you know a workaround to reach the same using Vaadin tree component.

Thank you for any suggestions.

Hi!

Currently there are no plans, but please create an enhancement ticket at
dev.vaadin.com
if you would like to see this feature in a future version.

As for a workaround, you can follow this algorithm to make your item the selected one:

  1. Find the item ID of the item you want selected
  2. Get the HierarchicalContainer and call getParent() and store them in a stack (or something) until you reach the root node
  3. Starting from the last node added to the stack (the root node) call Tree.expandItem(node) on each item in the stack
  4. Call Tree.setValue() with the item ID you want selected (see step 1)

After this has finished, you should have a tree that has expanded all the way to show your item and your item should be selected (hilighted).

HTH,
/Jonatan

Jonatan, thank you very much for your answer.

My question was about slightly other thing.

As I can see setValue does not influence on item visibility (I mean visibility for user). Imagine big tree, wanted item hidden by current view port, I make the following call:

tree.setValue(itemId);

As the result tree does not change state (scroll position and something else) and the wanted item is still hidden from user.

My question was how to ensure visible the hidden from user item.

Thanks in advance,
Anatoly Shirokov.

Well, one way to solve it is to extend the client side implementation, here is an example:


public class VMyTree extends VTree {

    @Override
    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
        super.updateFromUIDL(uidl, client);        
        Set<String> selectedIds = uidl.getStringArrayVariableAsSet("selected");
        if (selectedIds != null && !selectedIds.isEmpty()) {
            // Scroll selected item into view
            TreeNode selected = getNodeByKey(selectedIds.iterator().next());
            selected.scrollIntoView();
        }
    }

}

Thank you very much John! It is very helpfull. One more question. Why doesn’t Vaadin use such an implementation inside? As for me it is expected behavior.

I tried this. But some how tree scrolls back down after scrolling to the caption in a flash. Can you please suggest.