Expanding Rich tree nodes programmatically

Written by

in

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


	 
		        
	 

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 row = (TreeRowKey)rowKey;
			if(row.depth() == 1){
				state.expandNode(sampleTreeBinding, (TreeRowKey)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.

Comments

9 responses to “Expanding Rich tree nodes programmatically”

  1. Devika Avatar
    Devika

    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.

  2. omid Avatar

    Hi,

    Thanx, great post ..

  3. monu mishra Avatar

    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

    1. Greg Avatar

      Sorry, but I haven’t have a chance to check this on 4.0

  4. sritej Avatar
    sritej

    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..

    1. Greg Avatar

      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.

  5. Subodh Avatar
    Subodh

    Hi

    Can you please help me how to do it Richfaces4+ or primefaces5+

    1. Greg Avatar

      Hello

      Sorry, but I have not worked with richfaces / preimefaces in a while.

  6. Moab Mariz Meira Avatar
    Moab Mariz Meira

    thamk you so much. simple and elegant

Leave a Reply

Your email address will not be published. Required fields are marked *