Friday, January 23, 2015

Custom Selection Listener for ADF Table

In this post I will be describing how we can  have a custom selection listener for ADF Table. By default when we drag and drop a ADF Table Component, it gives us the option to select a row. If that option is selected, it creates a default selectionListener for the table component that is
#{bindings.VO.currentModel.makeCurrent"} where Iterator is VO iterator. This operation synchronizes the current selected row with the Iterator to which this Tree Binding is pointing to.

In Addition to this it also adds a attribute selectedRowKeys which has a value
#{bindings.VO.collectionModel.selectedRow}. This makes the selected row from the collection.

But when you change this to your custom selection handler of your managed bean, then the operation which are performed by {bindings.Iterator.currentModel.makeCurrent"} and selectedRowKeys needs to be done programmatically.

One way is to invoke #{bindings.VO.currentModel.makeCurrent"} programmatically in the selectionListener of the managed bean. This has been explained very clearly by my friend Ashish Awasthi :- Custom Selection Listener



In this article I will go through a different approach to implement custom selection listener.

Lets create a ADF project using HR Schema and create DepartmentsVO.

After that create a JSPX page and drag the VO as a table and select singe row selection. Now change the selectionListener to point to managed bean and create a selectionListener method. Also remove the selectedRowKeys attribute from the table as we will be doing this programmatically.

The Table should look like below:



In the managed bean, there are two methods. One is onTableRowSelection and customListener. 

customListener is what binded to the selectionListener from table component and onTableRowSelection method has actual logic to make the current row of the table as selected.

Below is the code in the bean.


After onTableRowSelection set the current Row , customListener gets the current row and access its department name.

Thats all. Lets run the page.



On Selecting Public Relations, we get output in the consoles as same name.


Thats it.

Application can be downloaded at Custom Table Selection Listener


Thanks





12 comments:

  1. Thank you for such a fantastic blog. Where else could anyone get that kind of info written in such a perfect way? I have a presentation that I am presently working on, and I have been on the lookout for such information.
    google watch

    ReplyDelete
  2. This works as expected.. Thanks

    ReplyDelete
  3. Hi Rohan,
    Nice Post. One update. Using Rich Component within the Managed Bean and Binding with UI Components can lead to potential memory leak. There are couple of other method/ways too get the updated row within the Manged Bean without doing Binding of UI Components

    ReplyDelete
  4. Hi Bish,
    Hope you are doing good as well as the project.
    Yes I understand your point, the post is just to demonstrate the Custom Selection Listener. Its a demo project and does not take care about advanced project level things. Its up to the developers for how to access bindings in bean !!

    Thanks

    ReplyDelete
  5. Thank you very much.. You save my time..

    ReplyDelete
  6. Hi Rohan

    couple for first images are not opening.

    ReplyDelete
    Replies
    1. Hi Waqar, Just checked again.All Images are opening fine. Kindly please check again.

      Thanks

      Delete
  7. Thanks for the blog.
    I created a custom selection listener based on the above example. My requirement restricts edit/delete for each row based on business logic. However, on making these changes, I face the below issues:

    a) when I add a new row, it is not displayed as selected as the listener is not invoked.
    b) when the table is loaded, none of the rows are selected by default.

    Can you please advise?

    ReplyDelete
    Replies
    1. Hi Kumarjit, Have you figured out the resolution for your questions.?

      Delete
  8. I have a table in page 1 which renders a af:table. It has cust id as one of its columns. On clicking the cust id, it navigates to page 2 which renders a table which also has cust id as one of its column. What i need is when i land on page2, the row that has cust id that i clicked on page 1 to be selected by default.

    ReplyDelete