`
dragonxiangfu
  • 浏览: 157143 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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,代码片段如下:

  1. publicList<Employees>employeesByLimit(intfirstRow,intmaxRow){
  2. StringqueryString="select*fromEmployeesorderbyemployee_idASC";
  3. returnem.createNativeQuery(queryString,
  4. Employees.class).setMaxResults(maxRow).setFirstResult(firstRow).getResultList();
  5. }
  6. /**
  7. *Returnstotalamountofrowsintable.
  8. *@returnTotalamountofrowsintable.
  9. */
  10. publicintemployeesTotalRows(){
  11. StringqueryString="select*fromEmployeesorderbyemployee_idASC";
  12. Queryquery=em.createNativeQuery(queryString);
  13. Listresults=query.getResultList();
  14. returnresults.size();
  15. }


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

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

代码片段:

  1. privateintfirstRow=0;
  2. privateintrowsPerPage=10;
  3. privateinttotalRows;
  4. privateinttotalPages;
  5. privateintcurrentPage=1;
  6. publicCustomPagination(){
  7. this.loadList();
  8. }
  9. publicvoidloadList(){
  10. /**
  11. *Returnstotalamountofrowsintable.
  12. *@returnTotalamountofrowsintable.
  13. */
  14. BindingContainerbindings=BindingContext.getCurrent().getCurrentBindingsEntry();
  15. AttributeBindingattr=(AttributeBinding)bindings.getControlBinding("EmployeesTotalRowCount");
  16. Stringval=attr.getInputValue().toString();
  17. introws=Integer.parseInt(val);
  18. this.setTotalRows(rows);
  19. doubleval1=((double)this.getTotalRows()/this.getRowsPerPage());
  20. inttotalPagesCal=(int)Math.ceil(val1);
  21. this.setTotalPages((totalPagesCal!=0)?totalPagesCal:1);
  22. }
  23. publicvoidfirstActionListener(ActionEventactionEvent){
  24. this.setCurrentPage(1);
  25. this.setFirstRow(0);
  26. }
  27. publicvoidpreviousActionListener(ActionEventactionEvent){
  28. this.setCurrentPage(this.getCurrentPage()-1);
  29. this.setFirstRow(this.getFirstRow()-this.getRowsPerPage());
  30. }
  31. publicvoidnextActionListener(ActionEventactionEvent){
  32. this.setCurrentPage(this.getCurrentPage()+1);
  33. this.setFirstRow(this.getFirstRow()+this.getRowsPerPage());
  34. }
  35. publicvoidlastActionListener(ActionEventactionEvent){
  36. this.setCurrentPage(this.getTotalPages());
  37. this.setFirstRow(this.getTotalRows()-
  38. ((this.getTotalRows()%this.getRowsPerPage()!=0)?this.getTotalRows()%
  39. this.getRowsPerPage():this.getRowsPerPage()));
  40. }
  41. publicbooleanisBeforeDisabled(){
  42. returnthis.getFirstRow()==0;
  43. }
  44. publicbooleanisAfterDisabled(){
  45. returnthis.getFirstRow()>=this.getTotalRows()-this.getRowsPerPage();
  46. }
  47. publicvoidsetFirstRow(intfirstRow){
  48. this.firstRow=firstRow;
  49. }
  50. publicintgetFirstRow(){
  51. returnfirstRow;
  52. }
  53. publicvoidsetRowsPerPage(introwsPerPage){
  54. this.rowsPerPage=rowsPerPage;
  55. }
  56. publicintgetRowsPerPage(){
  57. returnrowsPerPage;
  58. }
  59. publicvoidsetTotalRows(inttotalRows){
  60. this.totalRows=totalRows;
  61. }
  62. publicintgetTotalRows(){
  63. returntotalRows;
  64. }
  65. publicvoidsetTotalPages(inttotalPages){
  66. this.totalPages=totalPages;
  67. }
  68. publicintgetTotalPages(){
  69. returntotalPages;
  70. }
  71. publicvoidsetCurrentPage(intcurrentPage){
  72. this.currentPage=currentPage;
  73. }
  74. publicintgetCurrentPage(){
  75. returncurrentPage;
  76. }

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

1)创建Action绑定

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

  1. <variableType="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部分

  1. <af:panelGroupLayoutid="pgl9"layout="horizontal">
  2. <af:spacerwidth="10"height="10"id="s9"/>
  3. <af:panelGroupLayoutid="pgl10"inlineStyle="width:75px;"layout="horizontal">
  4. <af:outputTextvalue="EmployeedId"id="ot1"inlineStyle="font-weight:bold;"/>
  5. </af:panelGroupLayout>
  6. <af:spacerwidth="10"height="10"id="s7"/>
  7. <af:panelGroupLayoutid="pgl7"inlineStyle="width:75px;"layout="horizontal">
  8. <af:outputTextvalue="FirstName"id="ot6"inlineStyle="font-weight:bold;"/>
  9. </af:panelGroupLayout>
  10. <af:spacerwidth="10"height="10"id="s10"/>
  11. <af:panelGroupLayoutid="pgl11"inlineStyle="width:75px;"layout="horizontal">
  12. <af:outputTextvalue="LastName"id="ot4"inlineStyle="font-weight:bold;"/>
  13. </af:panelGroupLayout>
  14. <af:spacerwidth="10"height="10"id="s11"/>
  15. <af:panelGroupLayoutid="pgl12"inlineStyle="width:75px;"layout="horizontal">
  16. <af:outputTextvalue="Email"id="ot7"inlineStyle="font-weight:bold;"/>
  17. </af:panelGroupLayout>
  18. <af:spacerwidth="10"height="10"id="s12"/>
  19. <af:panelGroupLayoutid="pgl15"inlineStyle="width:75px;"layout="horizontal">
  20. <af:outputTextvalue="Salary"id="ot10"inlineStyle="font-weight:bold;"/>
  21. </af:panelGroupLayout>
  22. </af:panelGroupLayout>


2)表的数据

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


3)控制按钮

  1. <af:panelGroupLayoutid="pgl6">
  2. <af:commandButtontext="First"id="cb1"
  3. actionListener="#{CustomPagination.firstActionListener}"
  4. partialTriggers="i1"disabled="#{CustomPagination.beforeDisabled}"/>
  5. <af:commandButtontext="Prev"id="cb2"
  6. actionListener="#{CustomPagination.previousActionListener}"
  7. partialTriggers="i1"disabled="#{CustomPagination.beforeDisabled}"/>
  8. <af:commandButtontext="Next"id="cb3"
  9. actionListener="#{CustomPagination.nextActionListener}"
  10. partialTriggers="i1"disabled="#{CustomPagination.afterDisabled}"/>
  11. <af:commandButtontext="Last"id="cb4"
  12. actionListener="#{CustomPagination.lastActionListener}"
  13. partialTriggers="i1"disabled="#{CustomPagination.afterDisabled}"/>
  14. </af:panelGroupLayout>

6,运行代码

首页:

第三页:

尾页:



当页面需要显示的数据量比较大的时候,可以使用分页来简化用户的操作。但是在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,代码片段如下:

  1. publicList<Employees>employeesByLimit(intfirstRow,intmaxRow){
  2. StringqueryString="select*fromEmployeesorderbyemployee_idASC";
  3. returnem.createNativeQuery(queryString,
  4. Employees.class).setMaxResults(maxRow).setFirstResult(firstRow).getResultList();
  5. }
  6. /**
  7. *Returnstotalamountofrowsintable.
  8. *@returnTotalamountofrowsintable.
  9. */
  10. publicintemployeesTotalRows(){
  11. StringqueryString="select*fromEmployeesorderbyemployee_idASC";
  12. Queryquery=em.createNativeQuery(queryString);
  13. Listresults=query.getResultList();
  14. returnresults.size();
  15. }


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

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

代码片段:

  1. privateintfirstRow=0;
  2. privateintrowsPerPage=10;
  3. privateinttotalRows;
  4. privateinttotalPages;
  5. privateintcurrentPage=1;
  6. publicCustomPagination(){
  7. this.loadList();
  8. }
  9. publicvoidloadList(){
  10. /**
  11. *Returnstotalamountofrowsintable.
  12. *@returnTotalamountofrowsintable.
  13. */
  14. BindingContainerbindings=BindingContext.getCurrent().getCurrentBindingsEntry();
  15. AttributeBindingattr=(AttributeBinding)bindings.getControlBinding("EmployeesTotalRowCount");
  16. Stringval=attr.getInputValue().toString();
  17. introws=Integer.parseInt(val);
  18. this.setTotalRows(rows);
  19. doubleval1=((double)this.getTotalRows()/this.getRowsPerPage());
  20. inttotalPagesCal=(int)Math.ceil(val1);
  21. this.setTotalPages((totalPagesCal!=0)?totalPagesCal:1);
  22. }
  23. publicvoidfirstActionListener(ActionEventactionEvent){
  24. this.setCurrentPage(1);
  25. this.setFirstRow(0);
  26. }
  27. publicvoidpreviousActionListener(ActionEventactionEvent){
  28. this.setCurrentPage(this.getCurrentPage()-1);
  29. this.setFirstRow(this.getFirstRow()-this.getRowsPerPage());
  30. }
  31. publicvoidnextActionListener(ActionEventactionEvent){
  32. this.setCurrentPage(this.getCurrentPage()+1);
  33. this.setFirstRow(this.getFirstRow()+this.getRowsPerPage());
  34. }
  35. publicvoidlastActionListener(ActionEventactionEvent){
  36. this.setCurrentPage(this.getTotalPages());
  37. this.setFirstRow(this.getTotalRows()-
  38. ((this.getTotalRows()%this.getRowsPerPage()!=0)?this.getTotalRows()%
  39. this.getRowsPerPage():this.getRowsPerPage()));
  40. }
  41. publicbooleanisBeforeDisabled(){
  42. returnthis.getFirstRow()==0;
  43. }
  44. publicbooleanisAfterDisabled(){
  45. returnthis.getFirstRow()>=this.getTotalRows()-this.getRowsPerPage();
  46. }
  47. publicvoidsetFirstRow(intfirstRow){
  48. this.firstRow=firstRow;
  49. }
  50. publicintgetFirstRow(){
  51. returnfirstRow;
  52. }
  53. publicvoidsetRowsPerPage(introwsPerPage){
  54. this.rowsPerPage=rowsPerPage;
  55. }
  56. publicintgetRowsPerPage(){
  57. returnrowsPerPage;
  58. }
  59. publicvoidsetTotalRows(inttotalRows){
  60. this.totalRows=totalRows;
  61. }
  62. publicintgetTotalRows(){
  63. returntotalRows;
  64. }
  65. publicvoidsetTotalPages(inttotalPages){
  66. this.totalPages=totalPages;
  67. }
  68. publicintgetTotalPages(){
  69. returntotalPages;
  70. }
  71. publicvoidsetCurrentPage(intcurrentPage){
  72. this.currentPage=currentPage;
  73. }
  74. publicintgetCurrentPage(){
  75. returncurrentPage;
  76. }

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

1)创建Action绑定

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

  1. <variableType="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部分

  1. <af:panelGroupLayoutid="pgl9"layout="horizontal">
  2. <af:spacerwidth="10"height="10"id="s9"/>
  3. <af:panelGroupLayoutid="pgl10"inlineStyle="width:75px;"layout="horizontal">
  4. <af:outputTextvalue="EmployeedId"id="ot1"inlineStyle="font-weight:bold;"/>
  5. </af:panelGroupLayout>
  6. <af:spacerwidth="10"height="10"id="s7"/>
  7. <af:panelGroupLayoutid="pgl7"inlineStyle="width:75px;"layout="horizontal">
  8. <af:outputTextvalue="FirstName"id="ot6"inlineStyle="font-weight:bold;"/>
  9. </af:panelGroupLayout>
  10. <af:spacerwidth="10"height="10"id="s10"/>
  11. <af:panelGroupLayoutid="pgl11"inlineStyle="width:75px;"layout="horizontal">
  12. <af:outputTextvalue="LastName"id="ot4"inlineStyle="font-weight:bold;"/>
  13. </af:panelGroupLayout>
  14. <af:spacerwidth="10"height="10"id="s11"/>
  15. <af:panelGroupLayoutid="pgl12"inlineStyle="width:75px;"layout="horizontal">
  16. <af:outputTextvalue="Email"id="ot7"inlineStyle="font-weight:bold;"/>
  17. </af:panelGroupLayout>
  18. <af:spacerwidth="10"height="10"id="s12"/>
  19. <af:panelGroupLayoutid="pgl15"inlineStyle="width:75px;"layout="horizontal">
  20. <af:outputTextvalue="Salary"id="ot10"inlineStyle="font-weight:bold;"/>
  21. </af:panelGroupLayout>
  22. </af:panelGroupLayout>


2)表的数据

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


3)控制按钮

  1. <af:panelGroupLayoutid="pgl6">
  2. <af:commandButtontext="First"id="cb1"
  3. actionListener="#{CustomPagination.firstActionListener}"
  4. partialTriggers="i1"disabled="#{CustomPagination.beforeDisabled}"/>
  5. <af:commandButtontext="Prev"id="cb2"
  6. actionListener="#{CustomPagination.previousActionListener}"
  7. partialTriggers="i1"disabled="#{CustomPagination.beforeDisabled}"/>
  8. <af:commandButtontext="Next"id="cb3"
  9. actionListener="#{CustomPagination.nextActionListener}"
  10. partialTriggers="i1"disabled="#{CustomPagination.afterDisabled}"/>
  11. <af:commandButtontext="Last"id="cb4"
  12. actionListener="#{CustomPagination.lastActionListener}"
  13. partialTriggers="i1"disabled="#{CustomPagination.afterDisabled}"/>
  14. </af:panelGroupLayout>

6,运行代码

首页:

第三页:

尾页:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics