天天看點

ADF Table分頁

現象:

在開發群裡看到不少人在找ADF的分頁,雖然11gr2有自帶分頁,但之前的版本沒有,在 一定程度上不是很友善,本文基于疊代器Iterator做了一下例子

源碼:

1.managedBean部分

package view.backing;

import javax.faces.event.ActionEvent;

import model.vo.TablePageModelImpl;

import oracle.jbo.ViewObject;

import view.utils.ADFUtils;

public class BusinessBean {
    private int totalRows; //表中所有行數
    private int firstRecode; //每個記錄頁第一行
    private int currentPageNo = 1; //目前頁數
    private int totalPage; //基表被分成的所有頁數

    public BusinessBean() {
        //初始化 擷取所有行數
        TablePageModelImpl am =
            (TablePageModelImpl)ADFUtils.getApplicationModuleForDataControl("TablePageModelDataControl");
        ViewObject vo = am.getAdT13UserOpTypeVO();
        totalRows = (int)vo.getEstimatedRowCount();
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setFirstRecode(int firstRecode) {
        this.firstRecode = firstRecode;
    }

    public int getFirstRecode() {
        firstRecode = (this.getCurrentPageNo() - 1) * 10 + 1;
        return firstRecode;
    }

    public void setCurrentPageNo(int currentPageNo) {
        this.currentPageNo = currentPageNo;
    }

    public int getCurrentPageNo() {
        return currentPageNo;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotalPage() {
        totalPage =
                (this.totalRows % 10 == 0) ? (this.totalRows / 10) : ((this.totalRows /
                                                                       10) +
                                                                      1);
        return totalPage;
    }

    /**
     *首頁
     * @param actionEvent
     */
    public void first(ActionEvent actionEvent) {
        System.out.println(totalRows);
        this.setCurrentPageNo(1);
        this.getFirstRecode();
    }

    /**
     *下一頁
     * @param actionEvent
     */
    public void next(ActionEvent actionEvent) {
        if (this.getCurrentPageNo() < this.getTotalPage()) {
            this.setCurrentPageNo(this.getCurrentPageNo() + 1);
            this.getFirstRecode();
        }
    }

    /**
     *上一頁
     * @param actionEvent
     */
    public void previous(ActionEvent actionEvent) {
        if (this.getCurrentPageNo() > 1) {
            this.setCurrentPageNo(this.getCurrentPageNo() - 1);
            this.getFirstRecode();
        }
    }

    /**
     *尾頁
     * @param actionEvent
     */
    public void last(ActionEvent actionEvent) {
        this.setCurrentPageNo(this.getTotalPage());
        this.getFirstRecode();
    }

    /**
     *自選頁數
     * @param actionEvent
     */
    public void go(ActionEvent actionEvent) {
        this.setCurrentPageNo(this.getCurrentPageNo());
        this.getFirstRecode();
    }
}
           

2.頁面代碼

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
          xmlns:trh="http://myfaces.apache.org/trinidad/html">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1">
      <af:messages id="m1"/>
      <af:form id="f1">
        <af:panelGroupLayout id="pgl1" halign="center" layout="vertical"
                             styleClass="AFStretchWidth"
                             inlineStyle="width:900px;">
          <af:outputText value="ADF Table分頁執行個體" id="ot4"
                         inlineStyle="font-size:large; font-weight:bold;"/>
          <trh:tableLayout id="tl1" borderWidth="1" styleClass="AFStretchWidth"
                           width="800px">
            <trh:rowLayout id="rl2" inlineStyle="height:30px">
              <trh:cellFormat id="cf2" halign="center">
                <af:outputText value="OpTypeId" id="ot5"
                               inlineStyle="font-size:x-small; font-weight:bold;"/>
              </trh:cellFormat>
              <trh:cellFormat id="cellFormat4" halign="center">
                <af:outputText value="OpTypeCode" id="outputText4"
                               inlineStyle="font-size:x-small; font-weight:bold;"/>
              </trh:cellFormat>
              <trh:cellFormat id="cellFormat5" halign="center">
                <af:outputText value="OpTypeDesc" id="outputText5"
                               inlineStyle="font-size:x-small; font-weight:bold;"/>
              </trh:cellFormat>
              <trh:cellFormat id="cellFormat6" halign="center">
                <af:outputText value="OpTypeGroup" id="outputText6"
                               inlineStyle="font-size:x-small; font-weight:bold;"/>
              </trh:cellFormat>
            </trh:rowLayout>
            <af:iterator id="i1"
                         value="#{bindings.AdT13UserOpTypeVO.collectionModel}"
                         var="table" rows="10"
                         first="#{pageFlowScope.business.firstRecode}">
              <trh:rowLayout id="rl1" width="600px" inlineStyle="height:40px">
                <trh:cellFormat id="cf1" width="100px" halign="center">
                  <af:outputText value="#{table.OpTypeId}" id="ot1"/>
                </trh:cellFormat>
                <trh:cellFormat id="cellFormat1" width="350px" halign="center">
                  <af:outputText value="#{table.OpTypeCode}" id="outputText1"/>
                </trh:cellFormat>
                <trh:cellFormat id="cellFormat2" width="250px" halign="center">
                  <af:outputText value="#{table.OpTypeDesc}" id="outputText2"/>
                </trh:cellFormat>
                <trh:cellFormat id="cellFormat3" width="100px" halign="center">
                  <af:outputText value="#{table.OpTypeGroup}" id="outputText3"/>
                </trh:cellFormat>
              </trh:rowLayout>
            </af:iterator>
          </trh:tableLayout>
          <af:panelGroupLayout id="pgl2" layout="horizontal" halign="center">
            <af:commandButton text="首頁" id="cb1"
                              actionListener="#{pageFlowScope.business.first}"/>
            <af:commandButton text="下一頁" id="cb2"
                              actionListener="#{pageFlowScope.business.next}"/>
            <af:subform id="s1" defaultCommand="::cl1" default="true">
              <af:inputText id="it1" contentStyle="width:30px" autoSubmit="true"
                            value="#{pageFlowScope.business.currentPageNo}"/>
            </af:subform>
            <af:commandLink text="跳轉" id="cl1"
                            actionListener="#{pageFlowScope.business.go}"/>
            <af:commandButton text="上一頁" id="cb3"
                              actionListener="#{pageFlowScope.business.previous}"/>
            <af:commandButton text="尾頁" id="cb4"
                              actionListener="#{pageFlowScope.business.last}"/>
            <af:outputText value="#{pageFlowScope.business.currentPageNo}/" id="ot2"/>
            <af:outputText value="#{pageFlowScope.business.totalPage}" id="ot3"/>
          </af:panelGroupLayout>
        </af:panelGroupLayout>
      </af:form>
    </af:document>
  </f:view>
</jsp:root>
           

3.效果圖

ADF Table分頁
ADF Table分頁
ADF Table分頁
ADF Table分頁
ADF Table分頁
ADF Table分頁

結束

分頁的方法還有很多,僅供參考

繼續閱讀