Expand and Collapse an af:treeTable programmatically in ADF Faces

Sharing is Caring

Hello all,
this is a simple tutorial about expanding and collapsing an af:treeTable using managed bean code (programmatically)
Sometimes we need to expand or collapse treeTable automatically on some button action or on valueChangeEvent without touching the treeTable icons, In that case, we can make use of component binding to do this in the managed bean itself.

Steps to expand and collapse af:treeTable-

    • Create component binding of af:treeTable in the managed bean

 private RichTreeTable soTreeTableBind;
     public void setSoTreeTableBind(RichTreeTable soTreeTableBind) {
        this.soTreeTableBind = soTreeTableBind;
    }

    public RichTreeTable getSoTreeTableBind() {
        return soTreeTableBind;
    }

 

    • Now Call this method on any action to expand treeTable

private RowKeySet disclosedTreeRowKeySet = new RowKeySetImpl();

    /***Method to expand all tree table nodes*/
    private void expandTreeTable() {
        if (this.soTreeTableBind != null) {
            disclosedTreeRowKeySet = new RowKeySetImpl();
            CollectionModel model = (CollectionModel)soTreeTableBind.getValue();
            JUCtrlHierBinding treeBinding = (JUCtrlHierBinding)model.getWrappedData();
            JUCtrlHierNodeBinding rootNode = treeBinding.getRootNodeBinding();
            disclosedTreeRowKeySet = soTreeTableBind.getDisclosedRowKeys();
            if (disclosedTreeRowKeySet == null) {
                disclosedTreeRowKeySet = new RowKeySetImpl();
            }
            List<JUCtrlHierNodeBinding> firstLevelChildren = rootNode.getChildren();
            for (JUCtrlHierNodeBinding node : firstLevelChildren) {
                ArrayList list = new ArrayList();
                list.add(node.getRowKey());
                disclosedTreeRowKeySet.add(list);
                expandTreeChildrenNode(soTreeTableBind, node, list);
            }
            soTreeTableBind.setDisclosedRowKeys(disclosedTreeRowKeySet);
        }
    }
/**Method to expand childs*/
    private void expandTreeChildrenNode(RichTreeTable rt, JUCtrlHierNodeBinding node, List<Key> parentRowKey) {
        ArrayList children = node.getChildren();
        List<Key> rowKey;
        if (children != null) {
            for (int i = 0; i < children.size(); i++) {
                rowKey = new ArrayList<Key>();
                rowKey.addAll(parentRowKey);
                rowKey.add(((JUCtrlHierNodeBinding)children.get(i)).getRowKey());
                disclosedTreeRowKeySet.add(rowKey);
                if (((JUCtrlHierNodeBinding)(children.get(i))).getChildren() == null)
                    continue;
                expandTreeChildrenNode(rt, (JUCtrlHierNodeBinding)(node.getChildren().get(i)), rowKey);
            }
        }
    }

 

    • To collapse treeTable, use this code snippet on any event

    /**Collapse TreeTable
     * @param actionEvent
     */
    public void collapseTreeTableAction(ActionEvent actionEvent) {
        soTreeTableBind.getDisclosedRowKeys().clear();
        AdfFacesContext.getCurrentInstance().addPartialTarget(soTreeTableBind);
    }

Cheers 🙂 Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

3 thoughts on “Expand and Collapse an af:treeTable programmatically in ADF Faces”

Leave a Reply

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