ADF计算Table列(二)

在此之前写过一篇关于如何计算Table列的博客,参见http://blog.csdn.net/ygj26/article/details/6902767

后又发现一种不错的方法(来自Jheadstart),和大家分享一下!

工程代码请到 http://www.jdeveloper.com.cn/forum.php?mod=viewthread&tid=12&extra=page%3D1 下载

以HR中的Employees表为例,对表中的Salary字段进行汇总求和:

1.创建Manage Bean TableBean.java

 

[java]  view plain copy
  1. package view;  
  2.   
  3. import java.sql.SQLException;  
  4.   
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. import oracle.adf.view.rich.component.rich.data.RichTable;  
  9.   
  10. import oracle.jbo.domain.Number;  
  11.   
  12. public class TableBean {  
  13.       
  14.     private RichTable _table;  
  15.       
  16.     public TableBean() {  
  17.         super();  
  18.     }  
  19.       
  20.     /** 
  21.      * 用户对指定的列进行求和,如 #{requestScope.tableBean.sumRow['Salary']} 
  22.      */  
  23.     private Map sumRow = new HashMap() {  
  24.   
  25.         @Override  
  26.         public Object get(Object key) {  
  27.             Number total = new Number(0);  
  28.             for(int i=0; i<getTable().getRowCount(); i++) {  
  29.                 Map rowData = (Map)getTable().getRowData(i);  
  30.                 if(rowData != null) {  
  31.                     Object objectValue = rowData.get(key);  
  32.                     if(objectValue == null) {  
  33.                         continue;  
  34.                     }  
  35.                     Number value = null;  
  36.                     if(objectValue instanceof Number) {  
  37.                         value = (Number)objectValue;  
  38.                     } else {  
  39.                         try {  
  40.                             value = new Number(objectValue);  
  41.                         } catch (SQLException e) {  
  42.                             e.printStackTrace();  
  43.                         }  
  44.                     }  
  45.                     if(value != null) {  
  46.                         total = total.add(value);  
  47.                     }  
  48.                 }  
  49.             }  
  50.             return total;  
  51.         }  
  52.     };  
  53.   
  54.     public void setTable(RichTable _table) {  
  55.         this._table = _table;  
  56.     }  
  57.   
  58.     public RichTable getTable() {  
  59.         return _table;  
  60.     }  
  61.   
  62.     public void setSumRow(Map sumRow) {  
  63.         this.sumRow = sumRow;  
  64.     }  
  65.   
  66.     public Map getSumRow() {  
  67.         return sumRow;  
  68.     }  
  69. }  


2. 将TableBean配置到Manage Bean

[html]  view plain copy
  1. <managed-bean id="__4">  
  2.     <managed-bean-name id="__3">EmployeeTableBean</managed-bean-name>  
  3.     <managed-bean-class id="__2">view.TableBean</managed-bean-class>  
  4.     <managed-bean-scope id="__1">request</managed-bean-scope>  
  5.   </managed-bean>  

 

3. 在页面创建Table,并和Manage Bean绑定,注意binding="#{EmployeeTableBean.table}"<af:outputText id="ot5" value="#{EmployeeTableBean.sumRow['Salary']}"/>

[html]  view plain copy
  1. <af:table value="#{bindings.EmployeesView1.collectionModel}"  
  2.                      var="row" rows="#{bindings.EmployeesView1.rangeSize}"  
  3.                      emptyText="#{bindings.EmployeesView1.viewable ? 'No data to display.' : 'Access Denied.'}"  
  4.                      fetchSize="#{bindings.EmployeesView1.rangeSize}"  
  5.                      rowBandingInterval="0" id="resId1"  
  6.                      binding="#{EmployeeTableBean.table}"  
  7.                      partialTriggers="::cb1"> <!--中间的部分内容省略-->  
  8.              <af:column sortProperty="Salary" sortable="false"  
  9.                         headerText="#{bindings.EmployeesView1.hints.Salary.label}"  
  10.                         id="resId1c4">  
  11.                <af:outputText value="#{row.Salary}" id="ot2">  
  12.                  <af:convertNumber groupingUsed="false"  
  13.                                    pattern="#{bindings.EmployeesView1.hints.Salary.format}"/>  
  14.                </af:outputText>  
  15.                <f:facet name="footer">  
  16.                  <af:outputText id="ot5"  
  17.                                 value="#{EmployeeTableBean.sumRow['Salary']}"/>  
  18.                </f:facet>  
  19.              </af:column>  
  20.            </af:table>  

 

4.查看结果

结果1:

 

结果2:

转自:http://blog.csdn.net/ygj26/article/details/7334228

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值