ADF:TABLE – Get Selected Row or Rows

ADF:TABLE – Get Selected Row or Rows

ADF does an excellent job of making short work binding data to rich UI components. One of the most common use cases beyond basic presentment of data, is the ability to allow a user to select a row or rows of data presented and then act on it in some way.

We are going to look at both scenarios by way of a generic example, allowing us to perform both single and multiple row selection, then perform an action on the basis of the data item involved with the selection.

Our ADF Table Row Selection sample application uses a Placeholder Data Control that exposes some sample support issues for use in our JSPX page.

How to Get a Selected Row or Rows in an ADF Table – Our Example
The following explanation describes the steps taken in the sample application (source at bottom of post) and highlights the key areas of ADF that allow us to access this data from our components within a view (page). To create our sample application we

  1. Created a JSPX page SupportIncidents.jspx and made sure that we let JDeveloper “Automatically Expose UI Components in a New Managed Bean”
  2. Dragged and dropped the OpenSupportItems node from the SampleData Data Control onto our JSPX page and selected to represent the data as a ADF-Read Only Table that allows filtering, sorting and Multiple Rows row selection.
  3. After getting the information from the Data Control onto the page – which in and of itself offers rich UI handling, we now needed to get 2 buttons onto the page. To do this we dragged and dropped 2 buttons from the Resource Pallete from Common Components under ADF Faces
  4. Now is when the rubber meets the road and we dive into our backing bean to respond to actions taken on the buttons by end users. To do this we right click on out buttons and make sure to “Create Method Binding for Action”. This will generate a place within the backing bean to hold our logic to respond to a button click. We did this for both buttons, as they will use sample logic to illustrate getting all items selected in the table vs a single item

Once we accomplished the above, we were ready to write our backing bean code. In our backing code we access information created by ADF when we dragged and dropped part of our Data Control onto the page. When attempting to retrieve a selected value(s) from our table we need to locate the binding that was applied to the ADF Faces component. Where can we find this?

When we open our JSPX page we can view it in 5 ways – Design, Source, Bindings, Preview and History. The bindings tabs holds the key to what is needed to find our bindings in the backing bean and use in the following line

bindings.findIteratorBinding("OpenSupportItemsIterator")

Now that we know the magic designation to get the specific binding for our table data, let’s look at the two buttons in our sample and how we can get a single selected value or get multiple selected values (when a user Ctrl right clicks many rows or Shift clicks many rows).

Method Binding for Action for Button 1 – Getting a Single Selected Value

    public String cbSelectOne_action() {
        // For learning purposes - show Select One button clicked
        System.out.println("Select One Button has been Clicked");

        // Get bindings associated with the current scope, then access the one that we have assigned to our table - e.g. OpenSupportItemsIterator
        DCBindingContainer bindings =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding dcItteratorBindings =
            bindings.findIteratorBinding("OpenSupportItemsIterator");

        // Get an object representing the table and what may be selected within it
        ViewObject voTableData = dcItteratorBindings.getViewObject();

        // Get selected row
        Row rowSelected = voTableData.getCurrentRow();

        // Display attribute of row in console output - would generally be bound to a UI component like a Label and or used to call another proces
        System.out.println(rowSelected.getAttribute("IssueID"));

        return null;
    }

Method Binding for Action for Button 2 – Getting a Multiple Selected Values

    public String cbSelectMany_action() {
        // For learning purposes - show Select Many Button clicked
        System.out.println("Select Many Button has been Clicked");

        // RowKeySet Object can hold the selected rows from a user as follows
        RowKeySet rksSelectedRows =
            this.getTsupportIssues().getSelectedRowKeys();

        // Iterator object provides the ability to use hasNext(), next() and remove() against the selected rows
        Iterator itrSelectedRows = rksSelectedRows.iterator();

        // Get the data control that is bound to the table - e.g. OpenSupportItemsIterator
        DCBindingContainer bindings =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding dcIteratorBindings =
            bindings.findIteratorBinding("OpenSupportItemsIterator");

        // Information from binding that is specific to the rows
        RowSetIterator rsiSelectedRows =
            dcIteratorBindings.getRowSetIterator();

        // Loop through selected rows
        while (itrSelectedRows.hasNext()) {

            // Get key for selected row
            Key key = (Key)((List)itrSelectedRows.next()).get(0);

            // Use the key to get the data from the above binding that is related to the row
            Row myRow = rsiSelectedRows.getRow(key);

            // Display attribute of row in console output - would generally be bound to a UI component like a Label and or used to call another proces
            System.out.println(myRow.getAttribute("IssueID"));
        }
        
        return null;
    }

Download the ADF Table Row Selection sample application

I hope that this post has been able to save people some time when getting their feet wet with ADF. Once you understand this pattern ADF development can move very rapidly.

Special thanks to Zeeshan Baig and his ADF related blog posts which have been very helpful learning ADF development and unlocked some details around ADF data binding for me!

Related Articles

4 comments
ORACLEADF
ORACLEADF

Thx

God Bless You

خیلی باحال بود

anirban085
anirban085

Wonderful example. Worked perfect! Thank you.

pam182
pam182

està buena, gracias por la aportación

Trackbacks

  1. [...] This post was Twitted by JRSim_UIX [...]