Friday, November 2, 2007

Calculated Column in ADF Faces af:table Component

It happens that sometimes you need to implement calculated column in ADF Faces af:table column. Value for this column is calculated based on values in other columns. There could be many approaches to achieve this, as one of the most simple I can suggest to add calculated attribute into View object, on which af:table is based.

You can download developed sample application - CalculatedValue.zip. This sample is based on standard HR schema available in Oracle XE database.

Calculated attribute can be added to View object in two steps:

1. Define new object attribute on View object, just provide name and type:


New attribute will be created as Transient:


2. Generate View Row Class for View object, just by selecting check-box in View Object Editor:


In generated class, edit getSalaryDiff() method and put needed logic here:


When described two steps are done, in Data Control calculated value becomes available and you can use it in your application User Interface layer:


Implemented sample application provides table with calculated value included - difference between maximal and minimum salaries:


When running sample application, don't forget to add adf-faces-impl.jar and jsf-impl.jar to application's WEB-INF\lib directory.

10 comments:

  1. Hi Andrejus,

    that paper is quite interesting.

    What about one on calculated lines. Taking your examples, how would you implement a new line, at the end of the table, that will sum the 2 last attributes.

    Interesting uh ?

    ps : all samples downloads are dead.

    Regards.

    ReplyDelete
  2. Hi,

    It looks server is down, it will start to work after a while.

    If you need sample for this post, drop me email, i will forward you.

    Will be back to your question later today.

    Regards,
    Andrejus

    ReplyDelete
  3. But if i dont know np. of columns then how to add those.
    My use case is i have to add Total of each row in a "SubTotal" and each time row may be increased or deleted then corresponding "Subtotal is displaye"..
    How it can be possible
    Thanx in Advance

    ReplyDelete
  4. How can i add no. of columns that may be increased or decreased
    Not fix

    ReplyDelete
  5. You need to use ADF Dynamic table.

    Andrejus

    ReplyDelete
  6. Hi Andrejus,

    Very nice paper, Thank you.

    I have a question regarding refreshing value in calculated column.
    In my case, I have a table of users with 'Groups' column showing comma separated list of groups for each user.
    I use popup dialog to edit user records. User name refreshes after dialog is closed but 'Groups' column stay unchanged.
    I tried placing popup and dialog inside Partial triggers for 'Groups' column but it did not help.
    Could you please help me here?

    Thanks in advance,
    Vladimir

    ReplyDelete
  7. Hi,

    Try to re-execute iterator from dialog listener, may be transient attribute needs to be recalculated. Your calculated transient attribute is defined in ADF BC VO?

    Andrejus

    ReplyDelete
  8. I have a calculated Column of Date type in my table but sortable is not working in AF: table for that column. Please help

    ReplyDelete
  9. You should check if calculated attribute is included into SQL after sorting. If your attribute is not SQL based, I would not recommend to do in memory sorting, especially if there are many rows.

    Regards,
    Andrejus

    ReplyDelete
  10. Hi Andrejus,

    I have a requirement to search based on a calculated field.

    in order to achieve this I added transient variable in VO put the logic to concatenate in RowImpl and added item in search view criteria.

    although I am getting concatenated values in table but search did not work.

    Can you suggest how can I achieve this requirement?

    Thankyou
    Navpreet

    ReplyDelete