Thursday, August 4, 2016

Calculating Table Footer totals in ADF

Problem: To display total of numeric columns in ADF table footer . Sometimes requirement is to display total of a numeric columns , for ex. Total of the salaries of all employees in a search.

Solution: Their are several ways this can be achieved , one of the ways is described in this blog How To add Table Footer Totals. This blog mentions to write the iteration code inside the getter of the footer transient attribute in viewRowImpl class  , but if we have several attributes in table where we want to display the total in footer , above approach might be costly as iteration will be done per attribute. To avoid this we can use following approach.

1) Create a view object for your requirement and don't forget to generate Java classes .




2) Add a new transient attribute to your view object for storing "TotalSalary" . Create one new transient attribute for each field whose footer total needs to be displayed.


3) Define a variable for calculating the total in VOImpl class of the BigDecimal type. In case you have multiple attributes whose total needs to be displayed in footer , you need to declared variable for each attribute here. 


4) Override executeQuery() method of ViewObjectImpl . Call super.executeQuery() , and then
    call a method to calculate the totals and set it into the salaryTotal variable .  Now  we need to
    create a secondary row set iterator, iterate through it and calculate the total of the salary attribute,       don't forget to close the iterator :) . Here we can calculate total of multiple attributes in one go and       set them to the variables defined for them in VOImpl class.



5) Now go to viewRowImpl class and access the getter of variable defined in viewImpl class
from the getter of the footer total attribute. Please refer to below snapshot.


6) For UI, create a query panel with table for your view object ( do not create a column for transient attribute) .  Now inside the salary column , add footer facet with a binding of the transient attribute which was created for the footer total. You will need to add binding for the footer total  attribute to the page definition file.




7) Now run the application , and you should see the footer total for the salary column.
You can add same for multiple columns if your use case requires to do the same.





Please download the application from here . Cheers :) .



3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello,
    Thank you for the post. I am working on a similar functionality where I need to calculate total for each column under af:table. I used a groovy expression ( tried just for one column ) as starter for the transient variable added to master View object. The Groovy expression goes through viewlinkaccessor(on Drill down I get the page whose af:table is bound to child iterator View object) and calculates total for the column

    XXXLinkAccessor.sum("YYY")

    As usual I added this attribute through bindings to that column facet (footer)

    However I ran into to below issue and have corresponding question
    1. The total value in the column footer doesnt update when I use the table filter to filter the rows. Will this approach work in this case?
    2. In my usecase I believe I have to calculate the total in View Impl of the master View object, but is there a better way? (there are many child vos tied to this master view and many columns to calculate total for in the child view object)

    Thanks!

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete