Creating a dynamic (AJAX) Column Footer Summary in a Table Component using ADF Faces

6

One fairly common requirement for web applications is the display of Summary fields with calculated values. An obvious example is a table of multiple records with column-summaries appearing underneath the table. Using ADF Faces technology, it is fairly simple to quickly develop an application that presents a multi-record layout based on data retrieved from a database. In this article we will see how we can add a summary column to the columns in such a table layout – and to make those summaries automatically updating when a value for one of the records in the table is changed in the specific column.

 

....
 

Creating the master-detail application

The application I use for this example is a well-known one: a Master-Detail (form-table) page for DEPT and EMP. For each Department, we will see the details in a Form layout with underneath a Table component with all Employees in the Department.

Using JDeveloper 10.1.3, ADF BC, ADF Binding (Framework) and ADF Faces, creating such a page is almost trivial, especially if you generate it using JHeadstart 10.1.3 (which is what I did here). In quick summary the steps:

  1. Create new Application, choose Web Technology (ADF BC and JSF) as Technology Template; Model and ViewController project are created automatically
  2. Create Business Components from Tables EMP and DEPT in SCOTT schema in the Model project; add View Link from Source DeptView to Target EmpView
  3. Using JHeadstart: enable JHeadstart on the ViewController project, create default Application Definition file, generate the application
  4. Without JHeadstart: create a new JSF JSP page, drag and drop DeptView from the AppModule DataControl as Editable Form, drag and drop EmpView2 under DeptView to the jspx page.
  5. Run the Application to verify the data is shown and the master-detail coordination works as expected.

 

Add the Salary Summary to the DeptView ViewObject

We want this page to also contain the summary of all salaries in the currently selected Department. Where to put it is a later concern, let’s first get it on the page in the first place. The steps for this are:

1. Select the DeptView ViewObject and select Edit from the RMB menu

2. On the Attributes tab, press the New button to create a new (transient) attribute called SalSum. SalSum is of type Number, is Never updateable and is not mapped to Column or SQL

3. On the Java Tab, check the checkbox Generate Java File under View Row Class DeptViewRowImpl; also check the Accessors checkbox:

4. Press OK to close the VO Editor wizard.

5. From the RMB menu on the DeptView VO, select the option Goto View Row Class

 

Locate the Number getSalSum() accessor method. Replace the default implementation with this one:

  public Number getSalSum() {<br />    RowIterator emps = getEmpView();<br />    Number sum = new Number(0);<br />    while (emps.hasNext()) {<br />       sum = sum.add( (Number)emps.next().getAttribute(&quot;Sal&quot;));<br />    }<br />    return sum;<br />  }<br />&nbsp;

6. Add the SalarySum to the page either by dragging and dropping it from the DataControl Palette, or by synchronizing the Dept Group in the JHeadstart Application Definition editor and regenerating the application.

7. Run the application to inspect the newly added SalarySum. It should contain the correct sum of the salaries in the currently selected department. If you change the value of one of the salaries, it will currently not be updated automatically when you leave the field. It will however be synchronized for example when you sort the table by clicking one of the sortable column headers. 

Creating a proper Column Footer with the Salary Summary

The next step is to create the proper layout for the Salary Summary: we want to have it displayed in Footer underneath the Salary Column in our Table Component. The ADF Faces Column Component has a so called footer facet. We can use that facet to specify whatever should be rendered underneath the column. So we can implement the column footer facet for the Salary column, containing the SalarySummary, much like this:

 &lt;af:column sortable=&quot;true&quot; noWrap=&quot;true&quot;<br />            sortProperty=&quot;Sal&quot; formatType=&quot;number&quot;&gt;<br />   &lt;f:facet name=&quot;header&quot;&gt;<br />     &lt;h:panelGroup&gt;<br />       &lt;h:outputText value=&quot;Salary&quot;/&gt;<br />     &lt;/h:panelGroup&gt;<br />   &lt;/f:facet&gt;<br />   &lt;af:inputText id=&quot;DetailEmpSal&quot; value=&quot;#{row.Sal}&quot;<br />                 required=&quot;#{bindings.DetailEmpSal.mandatory}&quot;<br />                 rows=&quot;#{bindings.DetailEmpSal.displayHeight}&quot;<br />                 columns=&quot;#{bindings.DetailEmpSal.displayWidth}&quot;<br />                 maximumLength=&quot;10&quot; &gt;<br />     &lt;f:convertNumber groupingUsed=&quot;false&quot;<br />                      pattern=&quot;#{bindings.DetailEmpSal.format}&quot;/&gt;<br />   &lt;/af:inputText&gt;<br />   &lt;f:facet name=&quot;footer&quot;&gt;<br />     &lt;h:panelGroup&gt;<br />       &lt;af:outputText id=&quot;DeptEmpSalSum&quot;<br />                      value=&quot;#{bindings.DeptEmpSalSum.inputValue}&quot;&gt;<br />         &lt;f:convertNumber groupingUsed=&quot;false&quot;<br />     pattern=&quot;#{bindings.DetailEmpSal.format}&quot;/&gt;<br />       &lt;/af:outputText&gt;<br />     &lt;/h:panelGroup&gt;<br />   &lt;/f:facet&gt;<br /> &lt;/af:column&gt;<br />&nbsp;

However, it turns out that the Column Footer Facet gets only rendered if also the Table’s Footer Facet has been specified. Otherwise, the column footer is simply not rendered! The table footer facet is like this:

  &lt;f:facet name=&quot;footer&quot;&gt;<br />    &lt;af:outputText id=&quot;deptEmpTableFooter&quot; value=&quot;Summary:&quot;/&gt;<br />  &lt;/f:facet&gt;<br />&lt;/af:table&gt;<br />&nbsp;

Dynamically Refreshing the Salary Summary whenever a Salary value is changed

At this point, when the page is loaded, the Summary is displayed as expected. However, if we change the value of one of the salaries, the Summary is not immediately updated. Only when the table is refreshed for some other reason, such as sorting or detail disclosure/hide will the new summary value be shown. What we would like to have is an immediate update of the Summary whenever one of the Salaries is changed. We want to leverage the Partial Page Refresh mechanism of ADF Faces to help us realize this.

1. Let’s ensure that a change in a salary value causes an immediate submit to the server. We can easily do that by setting the autoSubmit attribute for the DetailEmpSal inputText component to true:

   &lt;af:inputText id=&quot;DetailEmpSal&quot; value=&quot;#{row.Sal}&quot;<br />                 required=&quot;#{bindings.DetailEmpSal.mandatory}&quot;<br />                 rows=&quot;#{bindings.DetailEmpSal.displayHeight}&quot;<br />                 columns=&quot;#{bindings.DetailEmpSal.displayWidth}&quot;<br />                 maximumLength=&quot;10&quot; autoSubmit=&quot;true&quot;&gt;<br />     &lt;f:convertNumber groupingUsed=&quot;false&quot;<br />                      pattern=&quot;#{bindings.DetailEmpSal.format}&quot;/&gt;<br />   &lt;/af:inputText&gt;<br />&nbsp;

2.  In order to update the SummarySal element upon processing the Partial Page Refresh caused by the Sal
ary change, we have to set the partia
lTriggers attribute.

However, here we run into a problem: we need to specify the ID of the component whose change should trigger the update of the Salary Sum in the partialTriggers attribute for the DeptEmpSalSum component. However, even though the ID for the Salary inputText component seems simple enough – DetailEmpSal – this is not the correct value! Since the Salary field will appear not once but once for every record in our table component and the ID needs to be unique, the actual ID will be different. Each Salary field will have an ID that includes DetailEmpSal as well as :0, :1, :2 etc. to make the ID values unique.

When I try a temporary workaround, just to see whether things are working, I run into a second issue: if I specify partialTriggers="DeptEmpDname" for the Column Footer Facet and/or the Table Footer Facet, it turns out that they are not in fact refreshed when PPR is processed. Only when I specify partialTriggers="DeptEmpDname" at the level of the Table Component will the Footer Facets be properly updated as part of the PPR processing cycle. That means I have now achieved that whenever I change one or more Salary values and I subsequently change the Department Name in the Master record, I get ‘dynamic, instantaneous’ update of the Salary Sum. Almost there, but not quite. By the way: what I am doing here is somewhat similar to Frank Nimphius’ blog article: ADF Faces: Compute totals of selected rows in a multi select table. He does not seem to have a problem with Table Footer refresh so perhaps I am doing something wrong here.

After consulting Frank and Duncan Mills, I am pointed in a new direction: programmatically specifying the targets of Partial Page Refresh. Sounds interesting, let’s try it out:

Programmatically specifying the targets of Partial Page Refresh

There is an API call – AdfFacesContext.getCurrentInstance().addPartialTarget(<the component to
refresh>); – that Duncan suggested to me. This should allow me to specify the Table – or perhaps even the Table and/or Column Footer Facet – that should be refreshed as part of the current PPR cycle. Of course, this call should be made whenever Salary has been changed. So using a ValueChangeListener, I should be well on my way. Should I not?

1. Create a new class EmpMgr to handle the Salary Changed event by adding the EmployeeTable to the list of partial targets:

import javax.faces.application.Application;<br />import javax.faces.component.UIComponent;<br />import javax.faces.context.FacesContext;<br />import javax.faces.event.ValueChangeEvent;<br /><br />import oracle.adf.view.faces.context.AdfFacesContext;<br /><br />public class EmpMgr {<br />    public EmpMgr() {<br />    }<br /><br />    public void HandleSalaryChangeEvent(ValueChangeEvent valueChangeEvent) {<br />        Application app = FacesContext.getCurrentInstance().getApplication();<br />        UIComponent table = (UIComponent)app.createValueBinding(&quot;#{DetailEmpCollectionModel.table}&quot;).getValue(FacesContext.getCurrentInstance());<br />        AdfFacesContext.getCurrentInstance().addPartialTarget(table);<br /><br />    }<br />}<br />&nbsp;

Note: the table component had its Binding property already set to #{DetailEmpCollectionModel.table}; I am simply reusing that binding. Also note that trying to use the DeptEmpSalSum outputText as partialTarget did not lead to a refresh: I had to use the table as refresh target.

2. Configure this new class as Managed Bean 

    ...<br />    &lt;managed-bean&gt;<br />        &lt;managed-bean-name&gt;EmpMgr&lt;/managed-bean-name&gt;<br />        &lt;managed-bean-class&gt;EmpMgr&lt;/managed-bean-class&gt;<br />        &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;<br />    &lt;/managed-bean&gt;<br />&lt;/faces-config&gt;<br />&nbsp;

3. Specify a ValueChangeListener for the Salary field in my Employees table:

&lt;af:inputText id=&quot;DetailEmpSal&quot; value=&quot;#{row.Sal}&quot;<br />                 required=&quot;#{bindings.DetailEmpSal.mandatory}&quot;<br />                 rows=&quot;#{bindings.DetailEmpSal.displayHeight}&quot;<br />                 columns=&quot;#{bindings.DetailEmpSal.displayWidth}&quot;<br />                 maximumLength=&quot;10&quot; autoSubmit=&quot;true&quot;<br />    valueChangeListener=&quot;#{EmpMgr.HandleSalaryChangeEvent}&quot;&gt;<br />  &lt;f:convertNumber groupingUsed=&quot;false&quot;<br />                   pattern=&quot;#{bindings.DetailEmpSal.format}&quot;/&gt;<br />&lt;/af:inputText&gt;<br />   &nbsp;<br />

4. Run the application, change a salary and keep your fingers crossed… IT WORKS!!!! (Thanks Frank and Duncan). Note: this opens up a lot of very interesting possibilities: we can determine dynamically on the server side which fields to be updated in the browser by simply adding them to the list of partialTargets.

Resources

ADF Faces: Compute totals of selected rows in a multi select table – Frank Nimphius’ Blogbuster Weblog 

ADF Faces Components Index on OTN 

ADF Faces Apache Incubator -  AF:Column component

Subversion Sources Repository for Apache MyFaces (Incubator) ADF Faces Sources 

 

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

6 Comments

  1. I need the same funtionality in 11g table in with jsff  and regions , 

    my outputText is not refresed(PPR ) on the column value submits 

     

  2. Thank you works very nice. I have one problem. When i have a new row in the table and i fill in the salary(in my case hours) then it doesnt refresh the total line. when ik refres the page the total does change. so it looks like the Partial Page Refresh does not work on new rows. do you have a solution for that problem?

  3. S. Gülçin Yücel on

    Thanks..thanks..thanks.. for your article.Very useful.But I had one problem.if I dont add emps.first() before return sum,sum value equals to last value of salary.Because of this I add emps.first().Now I have new problem.
    if table has range size,because of first() you cant see other pages.only show 1-10,what can I do about this problem???

  4. Thanks for your article. The solution solved the same problem I had. In my case, I have had the table binded to the CoreTable in the bean. So, only one line of code in the back bean valueChange handler to programmatically specifying the table as the target of PPR.