Saturday, September 22, 2012

af:treeTable custom Collapse All and Expand All buttons

To add the custom buttons to af:treeTable to Collapse All nodes and to Expand All nodes. Below are the steps


  1. Create a binding for treeTable in the backing bean.
  2. Add Two buttons For expand and collapse behaviors.
  3. Bind the button actionListeners to bean properties.
Use the below code in backing bean

    public void expandAll(ActionEvent actionEvent) {
        TreeModel model= (TreeModel) treeTable.getValue();
        JUCtrlHierBinding treeBinding = (JUCtrlHierBinding ) model.getWrappedData();
        ArrayList<JUCtrlHierNodeBinding> childList = (ArrayList<JUCtrlHierNodeBinding>)treeBinding.getChildren();
        List newKeys = new ArrayList();
        if(childList !=null){
            for(JUCtrlHierNodeBinding node: childList){
                newKeys.add(node.getKeyPath());
            }
        }
        treeTable.getDisclosedRowKeys().addAll(newKeys);
        AdfFacesContext.getCurrentInstance().addPartialTarget(treeTable);
    }

    public void collapseAll(ActionEvent actionEvent) {
        treeTable.getDisclosedRowKeys().clear();
        AdfFacesContext.getCurrentInstance().addPartialTarget(treeTable);
    }

Note: Above code works for first level of node expansion, if you need to expand all levels you need to iterate to add all keypaths. Also the range size iterator needs to be set to -1 if you need all rows to be expanded, otherwise only the rows in the current range will be expanded.

Complete example can be downloaded here.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...