Sunday, April 4, 2010

Dynamic Row Selection for ADF Table Component

It is a common use case, when user inserts new row into table and we want to prevent him from inserting one more row, before currently inserted row is not stored into database. Some customers like to have such functionality. You can implement this requirement with ADF pretty easily, just need to specify expression language for RowSelection property. I did a bit more and implemented complete use case, I will describe it today.

Download sample application - DynamicSelection.zip. This sample implements conditional check for Create button - it makes it disabled, when current row is a new row. I'm using ADF table clickToEdit functionality, this allows to improve table data rendering perfomance:


The problem with default implementation - user can select any other record and Create button will become enabled again, even user still didn't saved new record into database:


We want to prevent this and make sure Create button remains always disabled, when there is new (not yet saved) record available.  We can achieve this by substituting default RowSelection property value with expression language:


I will change single value to the following expression language statement:


You can see that I'm accessing custom method to get information about current row status (New, Unmodified, etc.) and if current row is not newly inserted row - selection will be enabled, otherwise it is disabled. Its a key thing - it will disable row selection, when there will be new row in the table (before it is saved).

Same custom method is used to disable/enable Create button itself:


Now, when there is new row - Create button remains disabled always and user cant select any other records, because table record selection is conditionally disabled:


When the record is saved - Create button becomes enabled and user can select records again:


Custom method to check if current row is a new row is coming from View Object implementation class:

25 comments:

Sharath Ram said...

Hi andrejus,

I have a simple af:table displaying results.
it's rowSelection="single" and editingMode="clickToEdit"

The normal behavior is that the user double clicks the row to edit the selected row entry.Works fine.

If I have a edit button to specifically do the same action(user selects a row,but now clicks on an edit button,without double clicking the row) , can i achieve it through bindings (as there is no edit operation in the data control operations) OR do I have to write a backing bean to make the row editable on click of the EDIT button?

Anonymous said...

Hi,

The

rowSelection="#{backingBeanScope.mainBean.createEnabled == 'New' ? 'none' : 'single'}"

causes all the rows to be disabled.
Am i doing something wrong ?

Cheers

Andrej Baranovskij said...

Hi,

It seems you misunderstood. Its how it should be.

Regards,
Andrejus

Unknown said...

Hi,
I am trying to generate View Object Interface in my appplication and i am ubable to do so.

Can you let me know how to generate View Object Interface.

Thanks
Ajay

Andrej Baranovskij said...

What you mean by "View Object Interface" ? There is not such term. May be you want to expose custom View Object Implementation method through Data Control?

Andrejus

Unknown said...

Yes, That's what i meant.

I am using your Dynamic Selection project and there i see that com.redsamurai.model.views.common.JobsView.java shows as "View Object Interface" in the JobsView(View Object).

So i wanted to generate the same in my application.

Thanks
Ajay

Andrej Baranovskij said...

This class will be auto generated, you only need to declare your custom method to be exposed to Data Control. Look into View Object Java section, there is option to expose your custom method.

Andrejus

Unknown said...

Somehow its not autogenerated in 11.1.1.4 Jdeveloper. Which version of Jdev are you using.

Does this get autogenerated while creating New VO ??

Andrej Baranovskij said...

I'm using all versions of JDev ;) And this feature is working very stable. VO interface is never created when you create VO. You must do these steps:

1. Go to Java section inside VO wizard
2. Create VO Java Implementation class, by editing Java options and clicking check box
3. Open generated Java implementation class, type your custom method
4. Back to Java section inside VO wizard, click option to expose your custom method
5. Select custom method and click OK
6. VO service interface will be generated now :)

Regards,
Andrejus

Unknown said...

After Step 2, selected all the checkbox but, a class like JobsView.java was not generated.

And the custom methods were not visible

Andrej Baranovskij said...

Okej, you are not following my steps :) I was saying after step 2 it will be created only VO Implementation class (Impl). You need to implement custom method by yourself. Please refer to ADF developer guide, View Objects section. I see you are missing background info, highly recommend to go through ADF developer guide, this will help to understand much faster.

Andrejus

Sumit Yadav said...

Hi Andrejus,
I did exactly the way mentioned.
But whenever i am clicking on Create New, all my rows get enabled not the newly created blank row ?
Any pointers .

Thanks
Sumit

Sumit Yadav said...

Hi Andrejus,
First of all, I would like express my thanks for all wonderful blogs you have written and going to write.
You are my favourate.

Coming to the problem I faced,One of colleague helped me in that issue, just wanted to share it .

We can create a transient attribute, the attribute value is derived from an Expression, the expression is this.object.getEntity(0).getEntityState(), which holds the state of the current row in the adf table.
And in the ready only property of that column I am checking the state and making it ediable accordingly .

Thanks
Sumit Yadav

Andrej Baranovskij said...

Yes, thats another possible way.

Andrejus

RiskBoys said...

Hi Andrejus,

I have editing mode as "clickToEdit" for my grid. There are mulitple editable columns in my grid with first column as select check box. As editing mode is "clickToEdit", I have to click thrice to select a row by checking check box. Is there any way i can override "clickToEdit" mode for a particular column?

Thanks,
Arpit

Andrej Baranovskij said...

Hi,

I believe in 11g R2, it should be possible.

Andrejus

Anonymous said...

Hi Andrejus,
Can I please know how to get the current row from a tree table in the VO Implementation class.

Andrej Baranovskij said...

You can try just to get current row of VO. It doesnt return correct result?

Andrejus

Sam said...

Hi Andrejus,

I am trying to create a clickToEdit table that works like in an auto-update mode i.e. it doesn't require the user to press any button to update the changed record?
To achieve so, is there any inbuilt feature of ADF, a property, that when set triggers the DB Update command and the Table component is refreshed, is applicable?


Many Thanks,
Sam

sourabh said...

Hi,
I am adding a new row to the table at the end and I need to select that row.How do i do that?

Unknown said...

Hi Andrejus,

I am having an ADF Table which has about 100 rows per page. So the page has a scroll bar. When i click on like 80th row, the row gets selected (rowSelection is single), but the page jumps to the top of the page.

Any suggestions where am i doing wrong

Thanks
Arun

DEMR said...

Hi, great info!!!

Unfortunately, it doesn't work in 12.1.3. When you create new row and then select another row, row status change and edit another row go on.

Any suggestion?

Thanks
DEMR

Andrej Baranovskij said...

I will need to test it with 12c.

Andrejus

DJ said...

Hi Andrej

I am new to ADF and this article seems good. I am working on a business request where based on a Status Attribute, if it is Yes, user should not be able to mark the checkbox checked. if N or other statuses user can. Status is visible on the adf table.

I am using ADF 12.1.1.
My approach was to add a changevaluelistener method to handle this but not getting a right way. Request you inputs to handle this.

Faizan said...

Hi DJ,
I have the same requirement.
Can you please share the solution if you have solved it.