Treetable - click on text to expand item - possible?

Hi
I have a tree table that I have to click on the little arrow to expand the item , then click again to expand the next level and so on
I would like to click on the text and for all the children to expand.
Is that possible? can some one tell me how?

thanks
James

Hi James,

let me use the example found here https://vaadin.com/docs/-/part/framework/components/components-treetable.html to help me demonstrate some ideas.

Instead of using a String object for the “Name” property you could instead use a button

ttable.addContainerProperty("Name", Button.class, null); This would allow you to add a ClickListener to the button to trigger the expansion. Buttons can be styled to look like labels, although this is not that simple. Another option is to use a ClickableLabel (https://vaadin.com/directory#!addon/clickablelabel) or any of the methods mentioned here: https://vaadin.com/forum/#!/thread/184075/184074

Once you are able to register a ClickEvent you can use the following method to exand all the descendants of an item

[code]
private void expand(int itemId) {
ttable.setCollapsed(itemId, false);

    @SuppressWarnings("unchecked")
    Collection<Integer> children = (Collection<Integer>) ttable
            .getChildren(itemId);
    if (children != null) {
        Iterator<Integer> childIterator = children.iterator();
        while (childIterator.hasNext()) {
            expand(childIterator.next());
        }
    }
}

[/code]This method will also allow you to modify the behaviour of “the little arrow” by adding an ExpandListener to the TreeTable

ExpandListener listener = new ExpandListener() { @Override public void nodeExpand(ExpandEvent event) { expand((int) event.getItemId()); } }; ttable.addExpandListener(listener); I hope this helps.

Best regards,

Henrik

Hi Henrick
Thanks for your reply

I did get the text expanding it’s child using the following - but didn’t go as far as opening all the children’s children

table.addItemClickListener(new ItemClickEvent.ItemClickListener() {
@Override
public void itemClick(ItemClickEvent event) {
if (table.isCollapsed(event.getItemId())) {
table.setCollapsed(event.getItemId(),false);
}
else {
table.setCollapsed(event.getItemId(),true);
}
// System.out.println("ITemClicked: " + event.getItemId());
}
});

Hi James,

OK so you went with a solution where you can click a row and it expands/collapses accordingly. To get all children to expand when clicking a row you just need to tweek my previous answer a litle bit. Change your itemClickListener to look like this:

        table.addItemClickListener(new ItemClickEvent.ItemClickListener() {
            @Override
            public void itemClick(ItemClickEvent event) {
                if (table.isCollapsed(event.getItemId())) {
                    expandAll(table, event.getItemId());
                } else {
                    table.setCollapsed(event.getItemId(), true);
                }
            }
        });

And add the expandAll method:

    private void expandAll(TreeTable table, Object itemId) {
        table.setCollapsed(itemId, false);
        Collection<?> children = table.getChildren(itemId);
        if (children != null) {
            for (Object child : children) {
                expandAll(table, child);
            }
        }
    }

This should work nicely.

}> Henrik

Excellent
Thanks