when click a tree item. how to select first tree item child node

when click a tree item. how to select first tree item child node

I try with
treeMenu.select(childNode.getId());
and
treeMenu.setValue( childNode.getId() );
treeMenu.setImmediate(true);

but the child item is n ot selected

thanks for any help

Yeah, according to API you select with the select(T) method.
https://vaadin.com/download/release/8.1/8.1.6/docs/api/com/vaadin/ui/Tree.html#select-T-
.

So if you initialize a Tree with for example.

Tree<Person> tree = new Tree<>(); Then you can select rows with person beans

tree.select(myPerson) where myPerson is of class Person.

Can you share a little more on your code.

  • Is treeMenu actually a instance of Tree and is it set up with some generic?
  • What class is childNode of?
  • What class is childNode.getId() of?
  • Does it work correctly with parent items and it is only the child items that don’t work?

thanks for your answer
I am using vaadin 7 tree so this constructor doesn’t work (can’t support parametrized tree)


Tree tree = new Tree<>();

I create the tree using this code

[color=#3498db]
//
treeMenu.setMultiSelect(false);
treeMenu.setSelectable(true);

    treeMenu.setSizeFull();        
    //
    treeMenu.addContainerProperty("caption", String.class, "");
    //
    treeMenu.addContainerProperty("object", Resource.class, "");
    // 
    treeMenu.addContainerProperty("icon", com.vaadin.server.Resource.class, "");    
    //
    treeMenu.setItemCaptionPropertyId("caption");
    treeMenu.setItemIconPropertyId("icon" );
    treeMenu.setItemCaptionMode(Tree.ITEM_CAPTION_MODE_PROPERTY);
    //
    treeMenu.setSelectable(true);

[/color]

I try to select child with this code

[color=#3498db]
public boolean selectTreeItem ( String childMenuCode, String parentMenuCode ) {

    if ( (childMenuCode != null) && ( ! childMenuCode.equals("")) && ( ! parentMenuCode.equals(childMenuCode) ) ) {
        Resource parentResource = resourceDao.findByName(parentMenuCode);
        Resource childResource = resourceDao.findByName(childMenuCode);
        if(childResource != null) {
           Item itemParent = treeMenu.getItem(parentResource.getId());    
           treeMenu.expandItem(itemParent);
           //    
          // boolean status = treeMenu.isSelected(selResource.getId());
          // boolean statusOrig = treeMenu.isSelected(selResourceOrig.getId());                    
           treeMenu.unselect(parentResource.getId());
           treeMenu.select(childResource.getId());
           //
           treeMenu.setValue( childResource.getId() );
           treeMenu.setImmediate(true);               
        }
        //
        return true;
     }        
    return false;

[/color]
}

thanks for any help

Here’s a minimal example on how to achieve auto selectin the first child when a parent is clicked and thus selected:



See the full sample on vaadinfiddle.com