ADF 11g: 表(af: table)分页

转自: http://blog.csdn.net/luyushuang/article/details/6756459

当页面需要显示的数据量比较大的时候,可以使用分页来简化用户的操作。但是在ADF 11g中,af:table并没有默认的分页功能,我们可以custom出JSPX页面的分页逻辑。

本例子使用的表是HR Sechema中的Employees。

2011/11/25 卢玉双 追加:

类似的实现方式,可以使用af:iterator,Table数据取自ADF BC的VO,也能够实现分页功能。


主要使用af:iterator这个tag,页面中数据的展示如同af:table,并且af:iterator可以基于table绑定进行循环显示数据集合。

1,基于Employees表创建entities,然后创建一个stateless session bean,以及data control,代码片段如下:

[java]  view plain copy print ?
  1. public List<Employees> employeesByLimit(int firstRow, int maxRow) {  
  2.        String queryString = "select * from Employees order by employee_id ASC";  
  3.        return em.createNativeQuery(queryString,  
  4.                                    Employees.class).setMaxResults(maxRow).setFirstResult(firstRow).getResultList();  
  5.   }  
  6.   
  7. /** 
  8.  * Returns total amount of rows in table. 
  9.  * @return Total amount of rows in table. 
  10.  */  
  11.   public int employeesTotalRows() {  
  12.        String queryString = "select * from Employees order by employee_id ASC";  
  13.        Query query = em.createNativeQuery(queryString);  
  14.        List results = query.getResultList();  
  15.        return results.size();  
  16.   }  


2,在ViewController层创建JSPX页面CustomPagination.jspx

3,创建页面对应的sessionScope级别的managed bean

代码片段:

[java]  view plain copy print ?
  1. private int firstRow = 0;  
  2. private int rowsPerPage = 10;  
  3. private int totalRows;  
  4. private int totalPages;  
  5. private int currentPage = 1;  
  6.   
  7. public CustomPagination() {  
  8.     this.loadList();  
  9. }  
  10.   
  11. public void loadList() {  
  12.     /** 
  13.      * Returns total amount of rows in table. 
  14.      * @return Total amount of rows in table. 
  15.      */  
  16.     BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();  
  17.     AttributeBinding attr = (AttributeBinding)bindings.getControlBinding("EmployeesTotalRowCount");  
  18.     String val = attr.getInputValue().toString();  
  19.     int rows = Integer.parseInt(val);  
  20.     this.setTotalRows(rows);  
  21.   
  22.     double val1 = ((double)this.getTotalRows() / this.getRowsPerPage());  
  23.     int totalPagesCal = (int)Math.ceil(val1);  
  24.     this.setTotalPages((totalPagesCal != 0) ? totalPagesCal : 1);  
  25.   
  26. }  
  27.   
  28. public void firstActionListener(ActionEvent actionEvent) {  
  29.     this.setCurrentPage(1);  
  30.     this.setFirstRow(0);  
  31. }  
  32.   
  33. public void previousActionListener(ActionEvent actionEvent) {  
  34.     this.setCurrentPage(this.getCurrentPage() - 1);  
  35.     this.setFirstRow(this.getFirstRow() - this.getRowsPerPage());  
  36. }  
  37.   
  38. public void nextActionListener(ActionEvent actionEvent) {  
  39.     this.setCurrentPage(this.getCurrentPage() + 1);  
  40.     this.setFirstRow(this.getFirstRow() + this.getRowsPerPage());  
  41.   
  42. }  
  43.   
  44. public void lastActionListener(ActionEvent actionEvent) {  
  45.     this.setCurrentPage(this.getTotalPages());  
  46.     this.setFirstRow(this.getTotalRows() -  
  47.                      ((this.getTotalRows() % this.getRowsPerPage() != 0) ? this.getTotalRows() %  
  48.                       this.getRowsPerPage() : this.getRowsPerPage()));  
  49. }  
  50.   
  51. public boolean isBeforeDisabled() {  
  52.     return this.getFirstRow() == 0;  
  53. }  
  54.   
  55. public boolean isAfterDisabled() {  
  56.     return this.getFirstRow() >= this.getTotalRows() - this.getRowsPerPage();  
  57. }  
  58.   
  59. public void setFirstRow(int firstRow) {  
  60.     this.firstRow = firstRow;  
  61. }  
  62.   
  63. public int getFirstRow() {  
  64.     return firstRow;  
  65. }  
  66.   
  67. public void setRowsPerPage(int rowsPerPage) {  
  68.     this.rowsPerPage = rowsPerPage;  
  69. }  
  70.   
  71. public int getRowsPerPage() {  
  72.     return rowsPerPage;  
  73. }  
  74.   
  75. public void setTotalRows(int totalRows) {  
  76.     this.totalRows = totalRows;  
  77. }  
  78.   
  79. public int getTotalRows() {  
  80.     return totalRows;  
  81. }  
  82.   
  83. public void setTotalPages(int totalPages) {  
  84.     this.totalPages = totalPages;  
  85. }  
  86.   
  87. public int getTotalPages() {  
  88.     return totalPages;  
  89. }  
  90.   
  91. public void setCurrentPage(int currentPage) {  
  92.     this.currentPage = currentPage;  
  93. }  
  94.   
  95. public int getCurrentPage() {  
  96.     return currentPage;  
  97. }  

 4,打开CustomPagination页面的数据绑定

1)创建Action绑定

2)在variableIterator标签下添加下面代码

[html]  view plain copy print ?
  1. <variable Type="int" Name="employeesTotalRows_Return" IsQueriable="false" IsUpdateable="0" DefaultValue="${bindings.employeesTotalRows.result}"/>  

3)创建引用employeesTotalRows_Return的属性绑定


4)创建调用employeesByLimit的Action绑定

5)创建Tree绑定给af:iterator使用

6)创建invokeAction,在页面的prepareMode阶段调用employeesTotalRows Action

7)最终,页面绑定如下图

5,修改页面代码

1)表的Title部分

[html]  view plain copy print ?
  1. <af:panelGroupLayout id="pgl9" layout="horizontal">  
  2.             <af:spacer width="10" height="10" id="s9"/>  
  3.             <af:panelGroupLayout id="pgl10" inlineStyle="width:75px;" layout="horizontal">  
  4.                 <af:outputText value="Employeed Id" id="ot1" inlineStyle="font-weight:bold;"/>  
  5.             </af:panelGroupLayout>  
  6.             <af:spacer width="10" height="10" id="s7"/>  
  7.             <af:panelGroupLayout id="pgl7" inlineStyle="width:75px;" layout="horizontal">  
  8.                 <af:outputText value="First Name" id="ot6" inlineStyle="font-weight:bold;"/>  
  9.             </af:panelGroupLayout>  
  10.             <af:spacer width="10" height="10" id="s10"/>  
  11.             <af:panelGroupLayout id="pgl11" inlineStyle="width:75px;" layout="horizontal">  
  12.                 <af:outputText value="Last Name" id="ot4" inlineStyle="font-weight:bold;"/>  
  13.             </af:panelGroupLayout>  
  14.             <af:spacer width="10" height="10" id="s11"/>  
  15.             <af:panelGroupLayout id="pgl12" inlineStyle="width:75px;" layout="horizontal">  
  16.                 <af:outputText value="Email" id="ot7" inlineStyle="font-weight:bold;"/>  
  17.             </af:panelGroupLayout>  
  18.             <af:spacer width="10" height="10" id="s12"/>  
  19.             <af:panelGroupLayout id="pgl15" inlineStyle="width:75px;" layout="horizontal">  
  20.                 <af:outputText value="Salary" id="ot10" inlineStyle="font-weight:bold;"/>  
  21.             </af:panelGroupLayout>  
  22.         </af:panelGroupLayout>  


2)表的数据

[html]  view plain copy print ?
  1. <af:iterator id="i1" value="#{bindings.result.collectionModel}" var="row">  
  2.             <af:panelGroupLayout id="pgl2" layout="horizontal">  
  3.                 <af:spacer width="10" height="10" id="s3"/>  
  4.                 <af:panelGroupLayout id="pgl3" layout="horizontal" inlineStyle="width:75px;">  
  5.                     <af:outputText value="#{row.employeeId}" id="ot8"/>  
  6.                 </af:panelGroupLayout>  
  7.                 <af:spacer width="10" height="10" id="s13"/>  
  8.                 <af:panelGroupLayout id="pgl13" layout="horizontal" inlineStyle="width:75px;">  
  9.                     <af:outputText value="#{row.firstName}" id="ot11"/>  
  10.                 </af:panelGroupLayout>  
  11.                 <af:spacer width="10" height="10" id="s4"/>  
  12.                 <af:panelGroupLayout id="pgl4" layout="horizontal" inlineStyle="width:75px;">  
  13.                     <af:outputText value="#{row.lastName}" id="ot9"/>  
  14.                 </af:panelGroupLayout>  
  15.                 <af:spacer width="10" height="10" id="s6"/>  
  16.                 <af:panelGroupLayout id="pgl5" layout="horizontal" inlineStyle="width:75px;">  
  17.                     <af:outputText value="#{row.email}" id="ot2"/>  
  18.                 </af:panelGroupLayout>  
  19.                 <af:spacer width="10" height="10" id="s8"/>  
  20.                 <af:panelGroupLayout id="pgl8" inlineStyle="width:75px;" layout="horizontal">  
  21.                     <af:outputText value="#{row.salary}" id="ot3"/>  
  22.                 </af:panelGroupLayout>  
  23.             </af:panelGroupLayout>  
  24.             <af:spacer width="10" height="10" id="s1"/>  
  25.         </af:iterator>  


3)控制按钮

[html]  view plain copy print ?
  1. <af:panelGroupLayout id="pgl6">  
  2.     <af:commandButton text="First" id="cb1"  
  3.         actionListener="#{CustomPagination.firstActionListener}"  
  4.         partialTriggers="i1" disabled="#{CustomPagination.beforeDisabled}"/>  
  5.     <af:commandButton text="Prev" id="cb2"  
  6.         actionListener="#{CustomPagination.previousActionListener}"  
  7.         partialTriggers="i1" disabled="#{CustomPagination.beforeDisabled}"/>  
  8.     <af:commandButton text="Next" id="cb3"  
  9.         actionListener="#{CustomPagination.nextActionListener}"  
  10.         partialTriggers="i1" disabled="#{CustomPagination.afterDisabled}"/>  
  11.     <af:commandButton text="Last" id="cb4"  
  12.         actionListener="#{CustomPagination.lastActionListener}"  
  13.         partialTriggers="i1" disabled="#{CustomPagination.afterDisabled}"/>  
  14. </af:panelGroupLayout>  

6,运行代码

首页:

第三页:

尾页:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值