I have found two different methods for expanding nodes in Rich tree from code, they both take advantage of component binding and component state.
Tree Setup
<rich:tree value="#{ourbean.sampleTree}" binding="#{ourbean.sampleTreeBinding}" var="node" nodeFace="simple" id="someid" > <rich:treeNode type="simple" > <h:outputText value="#{node}"/> </rich:treeNode> </rich:tree> |
Only thing to note here is the binding
attribute that we setup in our backing bean.
protected org.richfaces.component.UITree sampleTreeBinding; |
for both solutions after we changed the sampleTree
we need to update the binding with new value whitch is
value="#{ourbean.sampleTree}"
// Make sure that we set the new TreeModel on current binding sampleTreeBinding.setValue(sampleTree); |
Solution 1 : Expanding all nodes
TreeState componentState = (TreeState) sampleTreeBinding.getComponentState(); try { componentState.expandAll(sampleTreeBinding); } catch (IOException e) { e.printStackTrace(); } |
This will expand all levels of the nodes.
Solution 2 : Expanding only nodes that meet our criteria
try { final TreeState state = (TreeState) sampleTreeBinding.getComponentState(); sampleTreeBinding.walk(FacesContext.getCurrentInstance(), new DataVisitor(){ @SuppressWarnings("unchecked") public void process(FacesContext context, Object rowKey, Object argument) throws IOException { TreeRowKey<Object> row = (TreeRowKey<Object>)rowKey; if(row.depth() == 1){ state.expandNode(sampleTreeBinding, (TreeRowKey<Object>)rowKey); } } }); } catch (IOException e) { e.printStackTrace(); } |
Here we are only expanding nodes at depth 1 but it could be anything. Idea is that we are walking the tree
and checking if we are interested in the doing something with that node if so then we can modify it here.
I hope this helps.
hi very good Article which solved my requirement. Thanks a lot for that.
And can u pls give code how to expand the selected node till its leaf node.
i have tried many times couldn’t get it.
Hi,
Thanx, great post ..
Hello Greg,
in richfaces4.0 TreeState class has been removed. Can you please tell me how to bind it without that or any other solution.
Thanks,
Monu
Sorry, but I haven’t have a chance to check this on 4.0
Hey I have one question regarding expanding node.
My requirement is :
I have inputText and tree structure. When I entered some thing on input text, tree should automatically expand based on value entered in input text.
for eg: A is the parent node and under A I have child node called “Awesome”.It I enter Awesome in input text, the tree should expand automatically and it has to show awesome node.
Is there any way to do this….
Thanks in Advance..
You could iterate via all the nodes and compare the node value to your textbox via ‘indexOf’ or something similar. This is what ‘Solution 2’ is doing except there the depth of the node is used instead of value.
So I would have a ‘Textbox’ with onkeyup listener that makes async call to expand the tree.
Hi
Can you please help me how to do it Richfaces4+ or primefaces5+
Hello
Sorry, but I have not worked with richfaces / preimefaces in a while.
thamk you so much. simple and elegant