Follow by Email

Friday, 13 May 2016

Get Selected records (Child/Parent) from POJO based ADF treeTable

Hello All
Previously I have posted about creating af:treeTable programmatically using POJO and this the next post in series

In this post I am talking about a very common requirement while using POJO based treeTable
How can we get selected records (child/parent) from POJO based treeTable ?

For this post I am extending same sample application, Till now we have seen that how can we create POJO based treeTable
Now we'll see that how to get it's selected records



Next is create af:treeTable component binding in managed bean


import oracle.adf.view.rich.component.rich.data.RichTreeTable;

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

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

    public RichTreeTable getTreeTableBind() {
        return treeTableBind;
    }

Added a button on page and created it's ActionListener in managed bean,
See code written in ActionListener to get selected records of af:treeTable


import org.apache.myfaces.trinidad.model.ChildPropertyTreeModel;
import org.apache.myfaces.trinidad.model.CollectionModel;
import org.apache.myfaces.trinidad.model.RowKeySet;
import org.apache.myfaces.trinidad.model.TreeModel;

import javax.faces.event.ActionEvent;



    /**Method to get Selected row value from POJO based treeTable
     * To Know about creation of POJO based tree
     * Check-http://www.awasthiashish.com/2014/07/populate-aftreetable-programatically.html
     * @param actionEvent
     */
    public void getSelectedRowAction(ActionEvent actionEvent) {
        //Get Collection Model from treeTable binding
        CollectionModel treeModel = null;
        treeModel = (CollectionModel) this.getTreeTableBind().getValue();
        //Get selected row keys from treeTable
        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();
                //Get Seasons (Parent) List of selected row
                Seasons s = seasonList.get(Integer.parseInt(val.get(0).toString()));
                //Get charaters list of selected seasons
                List<Seasons> characterList = s.getCharacters();
                //If selected record is child (Character)
                if (val.size() > 1) {
                    Seasons character = characterList.get(Integer.parseInt(val.get(1).toString()));
                    System.out.println("Charater Name-" + character.getName());
                } else {
                    System.out.println("Season Name-" + s.getName());
                }
            }
        }
    }

Run and check application , selected some records (both parent and child)


On button click - selected records are printed on console

Sample ADF Application - Download
Cheers :) Happy Learning

No comments :

Post a Comment