Saturday, March 19, 2011

Programmatically disclosing nodes in a <af:tree>

Programmatically disclosing nodes in a <af:tree> is a very common use case in Rich Inernet Applications. I'm sharing an example (built using ADf Faces) illustrating three different use cases on this topic.

1. Expand all nodes of a <af:tree> component
2. Collapse all nodes of <af:tree> component
3. Expand specific nodes of <af:tree> component


Download

You can download the sample workspace from here.
[Runs with Oracle JDeveloper 11g R1 PS3 + HR Schema]

A glance at the implementation

1. Expand all nodes of a <af:tree>

This disclosed behavior of a tree component is controlled by the DisclosedRowKeys of tree component. The below shown method make use of constructor RowKeySetTreeImpl(boolean addAll) to create instance of RowKeySet having all rowKeys added. Please note that, if the parameter 'addAll' is true, every rowKey is initially added to this RowKeySet. This effectively would result in disclosure of all nodes when the tree is displayed.

 public void expandAllTreeNodes(ActionEvent act) {  
   
  UIXTree tree = getTree();  
  RowKeySet _disclosedRowKeys = new RowKeySetTreeImpl(true);  
  _disclosedRowKeys.setCollectionModel(ModelUtils.toTreeModel(tree.getValue()));  
  tree.setDisclosedRowKeys(_disclosedRowKeys);  
   
 }    

2. Collapse all nodes of <af:tree> component

This is achieved by clearing off the DisclosedRowKeys of the tree component.

   
 public void collapseAllTreeNodes(ActionEvent act) {  
   
  UIXTree tree = getTree();  
  RowKeySet _disclosedRowKeys = tree.getDisclosedRowKeys();  
  if (_disclosedRowKeys != null && _disclosedRowKeys.size() > 0) {  
   _disclosedRowKeys.clear();  
  }  
  tree.setDisclosedRowKeys(_disclosedRowKeys);  
    
 }  

3. Expand specific nodes of <af:tree> component

The code searches for a specific node value by setting the Rowkey to an indexed value and iterate over nodes recursively.
Fusion Web UI Developers Guide Says:
The tree model is a collection of rows. It has an isContainer() method that returns true if the current row contains child rows. To access the children of the current row, you call the enterContainer() method. Calling this method results in the TreeModel instance changing to become a collection of the child rows. To revert back up to the parent collection, you call the exitContainer() method.

3 comments:

subu said...
This comment has been removed by the author.
subu said...

nice post,
in same tense.
i had some usecase. i cant achieve it.
you may give right direction.
here my usecase regards af:tree with links navigating to the page

>> configuration
|
>config1010 basicemployeeprofile
>config1020 salaryprofile
>> transaction
|
>trans1010 profile
>trans1020 salaryupdation

no problem with this tree.
my concept is simple.

when i click config1010 means it want to navigate to
config1010.jspx page or jsff.

when i click config1020 means it want to navigate to
config1020.jspx page or jsff.

here n most example, which i saw is , when i click the node means navigate to corresponding records.

here i want to navigate corresponding page.

so thing is simple the id that is config1010.
if config1010 is clicked means
navigate config1010.jspx page

Shrinivas Kunthe said...
This comment has been removed by the author.