Hello,
I was just wondering if there is an easy way to implement an expand event within a treetable?
The clickItem even doesnt trigger when you attempt to expand or collapse an item.
The TreeTable doesn’t currently support Expand/Collapse events like the Tree component. I guess those could be added quite easily. Patch or/and ticket is welcome
Depending on what you want to do on those events, it might be best for you to implement the Collapsible interface in your data source. Then you would get your container notified when node is collapsed/expanded. I would also make your code cleaner if you need to persist the tree state and help the TreeTable to scale better if you have vast amounts of data in your container.
I’ve been unable to get implementing Collapsible to work. I have a container that extends HierarchicalContainer and implements Collapsible. Implementation is simple. I have a set tracking expanded items as follows:
private Set<Item> expandedItems = new HashSet<Item>();
My implementation is:
public void setCollapsed(final Object itemId, final boolean collapsed) {
if (hasChildren(itemId)) {
if (collapsed) {
expandedItems.remove(itemId);
} else {
if (!expandedItems.contains(itemId)) {
expandedItems.add((Item) itemId);
}
}
}
}
public boolean isCollapsed(final Object itemId) {
if (hasChildren(itemId)) {
return !expandedItems.contains(itemId);
}
return true;
}
I verified that TreeTable is calling this implementation from CollapsibleStrategy. However, the result is a TreeTable where all the items are always expanded. Even when you click on an item and the little arrow toggles between a right arrow and a down arrow, the sub-items remain visible.
How do you go about submitting a patch? Here is my submission to add expand and collapse events in TreeTable with minimal changes. Somewhere declare the following:
private final com.vaadin.ui.Tree shadowTree = new com.vaadin.ui.Tree();
make to following changes to toggleChildVisibility:
private void toggleChildVisibility(final Object itemId) {
final ContainerStrategy cs = getContainerStrategy();
if (cs.isNodeOpen(itemId)) {
fireEvent(shadowTree.new CollapseEvent(this, itemId));
} else {
fireEvent(shadowTree.new ExpandEvent(this, itemId));
}
getContainerStrategy().toggleChildVisibility(itemId);
// ensure that page still has first item in page, ignore buffer refresh
// (forced in this method)
setCurrentPageFirstItemIndex(getCurrentPageFirstItemIndex());
requestRepaint();
}
That will work for the user to listen in on all component events and check if the event is a collapse event or expand event. To explicitly enable listening to those events add the following code to the class (just copied from Tree):
/**
* Adds the expand listener.
*
* @param listener
* the Listener to be added.
*/
public void addListener(final ExpandListener listener) {
addListener(ExpandEvent.class, listener, ExpandListener.EXPAND_METHOD);
}
/**
* Removes the expand listener.
*
* @param listener
* the Listener to be removed.
*/
public void removeListener(final ExpandListener listener) {
removeListener(ExpandEvent.class, listener, ExpandListener.EXPAND_METHOD);
}
/**
* Adds the collapse listener.
*
* @param listener
* the Listener to be added.
*/
public void addListener(final CollapseListener listener) {
addListener(CollapseEvent.class, listener, CollapseListener.COLLAPSE_METHOD);
}
/**
* Removes the collapse listener.
*
* @param listener
* the Listener to be removed.
*/
public void removeListener(final CollapseListener listener) {
removeListener(CollapseEvent.class, listener, CollapseListener.COLLAPSE_METHOD);
}