Please disable your adblock and script blockers to view this page

Search this blog

Thursday 19 May 2016

Traverse POJO based ADF treeTable, Get Child nodes of selected parent


Previosuly I have posted about populating af:treeTable programmatically and getting selected records from POJO based treeTable

This post is next in that series and here I am extending same sample application that show a tree of Seasons and it's characters
First we will see How to traverse a POJO based treeTable (Iterate through all nodes of treeTable)

Basic idea is to get treeTable Collection Model and iterate over it to get all nodes value, To get CollectionModel created treeTable component binding in managed bean



    //Component Binding of af:treeTable in bean
    private RichTreeTable treeTableBind;

    public void setTreeTableBind(RichTreeTable treeTableBind) {
        this.treeTableBind = treeTableBind;
    }

    public RichTreeTable getTreeTableBind() {
        return treeTableBind;
    }

and POJO based treeTable looks like this


Dropped a button page to traverse treeTable , Created ActionListener in Managed Bean

Code to Traverse POJO based treeTable:


    /**Method to iterate through all nodes of treeTable (parent and child both)
     * @param actionEvent
     */
    public void traverseTreeTableAction(ActionEvent actionEvent) {
        //Get TreeTable CollectionModel
        CollectionModel treeModel = null;
        treeModel = (CollectionModel) this.getTreeTableBind().getValue();
        //Creata an Object to RowKeys and collection model to it
        RowKeySet rks = new RowKeySetTreeImpl();
        rks.setCollectionModel(treeModel);
        rks.addAll();
        //Iterate Over RowKeySet to get all nodes
        for (Object o : rks) {
            treeModel.setRowKey(o);
            //Here treeTable shows list of Seasons objects
            Seasons season = (Seasons) treeModel.getRowData();
            System.out.println("  " + season.getName());
        }
    }


On button click, see output on console

Output on Console-



















Next is to traverse a selected parent node to get it's all child nodes

Code to get all child of selected node:


    /**Method to iterate through all childs of selected node of treeTable
     * @param actionEvent
     */
    public void traverseSelectedNodeTreeTableAction(ActionEvent actionEvent) {
        //Get Collection Model from treeTable binding
        CollectionModel treeModel = null;
        treeModel = (CollectionModel) this.getTreeTableBind().getValue();
        RowKeySet selectedChildKeys = getTreeTableBind().getSelectedRowKeys();
        if (!selectedChildKeys.isEmpty()) {
            List<Seasons> seasonList = (List<Seasons>) treeModel.getWrappedData();
            //Create iterator from RowKeySet
            Iterator selectedCharIter = selectedChildKeys.iterator();
            //Iterate over RowKeySet to get all selected childs of treeTable
            while (selectedCharIter.hasNext()) {
                List val = (List) selectedCharIter.next();
                //Check that selected node should be parent node
                if (val.size() == 1) {
                    //Get Seasons (Parent) List of selected row
                    Seasons s = seasonList.get(Integer.parseInt(val.get(0).toString()));
                    //Get character(Child) list of Seasons
                    List<Seasons> characters = s.getCharacters();
                    //Iterate over character list
                    Iterator iter = characters.iterator();
                    while (iter.hasNext()) {
                        Seasons sp = (Seasons) iter.next();
                        System.out.println(" "+sp.getName());
                    }
                }
            }
        }
    }


On button click, see output on console

Output on Console

Sample ADF Application- Download
Cheers :) Happy Learning

2 comments :

  1. How can i add node or child node Please

    ReplyDelete
    Replies
    1. Salah

      Check previous post in this series - http://www.awasthiashish.com/2014/07/populate-aftreetable-programatically.html
      Here treeTable is populated using a List and you can add record in that List

      Ashish

      Delete