ADF11g-008: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

 

package view;

import java.sql.SQLException;

import java.util.HashMap;
import java.util.Map;

import oracle.adf.view.rich.component.rich.data.RichTable;

import oracle.jbo.domain.Number;

public class TableBean {
    
    private RichTable _table;
    
    public TableBean() {
        super();
    }
    
    /**
     * 用户对指定的列进行求和,如 #{requestScope.tableBean.sumRow['Salary']}
     */
    private Map sumRow = new HashMap() {

        @Override
        public Object get(Object key) {
            Number total = new Number(0);
            for(int i=0; i<getTable().getRowCount(); i++) {
                Map rowData = (Map)getTable().getRowData(i);
                if(rowData != null) {
                    Object objectValue = rowData.get(key);
                    if(objectValue == null) {
                        continue;
                    }
                    Number value = null;
                    if(objectValue instanceof Number) {
                        value = (Number)objectValue;
                    } else {
                        try {
                            value = new Number(objectValue);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                    if(value != null) {
                        total = total.add(value);
                    }
                }
            }
            return total;
        }
    };

    public void setTable(RichTable _table) {
        this._table = _table;
    }

    public RichTable getTable() {
        return _table;
    }

    public void setSumRow(Map sumRow) {
        this.sumRow = sumRow;
    }

    public Map getSumRow() {
        return sumRow;
    }
}


2. 将TableBean配置到Manage Bean

<managed-bean id="__4">
    <managed-bean-name id="__3">EmployeeTableBean</managed-bean-name>
    <managed-bean-class id="__2">view.TableBean</managed-bean-class>
    <managed-bean-scope id="__1">request</managed-bean-scope>
  </managed-bean>

 

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

	<af:table value="#{bindings.EmployeesView1.collectionModel}"
                      var="row" rows="#{bindings.EmployeesView1.rangeSize}"
                      emptyText="#{bindings.EmployeesView1.viewable ? 'No data to display.' : 'Access Denied.'}"
                      fetchSize="#{bindings.EmployeesView1.rangeSize}"
                      rowBandingInterval="0" id="resId1"
                      binding="#{EmployeeTableBean.table}"
                      partialTriggers="::cb1"> <!--中间的部分内容省略-->
              <af:column sortProperty="Salary" sortable="false"
                         headerText="#{bindings.EmployeesView1.hints.Salary.label}"
                         id="resId1c4">
                <af:outputText value="#{row.Salary}" id="ot2">
                  <af:convertNumber groupingUsed="false"
                                    pattern="#{bindings.EmployeesView1.hints.Salary.format}"/>
                </af:outputText>
                <f:facet name="footer">
                  <af:outputText id="ot5"
                                 value="#{EmployeeTableBean.sumRow['Salary']}"/>
                </f:facet>
              </af:column>
            </af:table>

 

4.查看结果

结果1:

ADF11g-008:ADF计算Table列(二)_第1张图片

ADF11g-008:ADF计算Table列(二)_第2张图片

 

结果2:

ADF11g-008:ADF计算Table列(二)_第3张图片

ADF11g-008:ADF计算Table列(二)_第4张图片
 

 

你可能感兴趣的:(ADF11g-008:ADF计算Table列(二))