Wednesday, September 12, 2012

Custom SelectionListener for af:table with select many option. ADF BC 11g

In this example we are going to create a custom Selection Listener for an af:table.
Furthermore, we are going to investigate how the Selection Listener works in the case where the table gives the ability to the user to select more the one record.
After this example we will be able to understand what is going on in the above scenario.

Download Sample Application.

In this example we are going to use the HR schema and the table Countries. We will be using also JDeveloper

The Requirement:
We would like to have a table of Countries where, we will be able to select more than one rows (Countries).
While the selection process takes place, we would like to see a label on the side that keeps updating with the names of the countries that are being selected. Furthermore, when a row (country) is deselected, the label will be updated again to reflect the names properly.

Simple right?
Imagine, all the people.. living all in peaace ou ouo ououou..
Now that I have your attention..
First we will generate the Business components.
A quick refresher can be found here.

After that is done, we are going to create our simple page with the af:table from that View object (Countries)

In order to enable the select many on our table. It is quite easy. It is just a property in the table.

I know, you can do it, while dropping the iterator onto the page. I preferred to show the property instead. Good for you to notice!

Right. Now we have to set our selection listener.
This is quite easy:
Just locate the selection listener

All we have to do now is to setup our custom Selection Listener.
Ou custom listener will be written in JAVA method in a JAVA class. So we have to create a new JAVA class and a method inside it. It will take some time right?
No. JDeveloper takes care of those things for you. Check the picture below:

In the small window, you can press new and you JDeveloper will create for you all you need.
In this example I placed the names shown in the screen.
So now, we already have our class, properly configured, and a method that accepts a Selection Event.

Prior to implementing this, in the newly created java class, we are going to keep the names of the selected rows in our table. We are going to that in the class that holds our custom selection Listener. We are going to create an instance variable of type String with it's accessors:

That String is going to hold all the names of the rows we select or deselect at runtime. Cool isn't it?

Remember, we are on a select many  situation here..

A question first.
if you create a method in your Application Module that will return the current row of our ViewObject. What do you think that it will return when we select 5 rows and immediately call it?

I dare you to try it! :) (The code is there in the sample application).
Well, as far as I know, you can get only one row as the current one. So, there must be one of the selected. (it is.. randomly, it is one).

So how are we supposed to do a custom selection for many records?
Exactly the same way as we would have done for a single selection. Simple. The code exists (Check out the references below for the links).
The difference that we are going to do here is to get the selected ones and loop over them in order to get the information we want and display it on another output text.
Eventually we will be having something like the following:

OK, on to the code

As you can see, it is a small addition. You have access to the rowKeys and thus you have access to the rows.

Download Sample Application.



No comments:

Post a Comment


Related Posts Plugin for WordPress, Blogger...