转自: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,代码片段如下:
- publicList<Employees>employeesByLimit(intfirstRow,intmaxRow){
- StringqueryString="select*fromEmployeesorderbyemployee_idASC";
- returnem.createNativeQuery(queryString,
- Employees.class).setMaxResults(maxRow).setFirstResult(firstRow).getResultList();
- }
- /**
- *Returnstotalamountofrowsintable.
- *@returnTotalamountofrowsintable.
- */
- publicintemployeesTotalRows(){
- StringqueryString="select*fromEmployeesorderbyemployee_idASC";
- Queryquery=em.createNativeQuery(queryString);
- Listresults=query.getResultList();
- returnresults.size();
- }
2,在ViewController层创建JSPX页面CustomPagination.jspx
3,创建页面对应的sessionScope级别的managed bean
代码片段:
- privateintfirstRow=0;
- privateintrowsPerPage=10;
- privateinttotalRows;
- privateinttotalPages;
- privateintcurrentPage=1;
- publicCustomPagination(){
- this.loadList();
- }
- publicvoidloadList(){
- /**
- *Returnstotalamountofrowsintable.
- *@returnTotalamountofrowsintable.
- */
- BindingContainerbindings=BindingContext.getCurrent().getCurrentBindingsEntry();
- AttributeBindingattr=(AttributeBinding)bindings.getControlBinding("EmployeesTotalRowCount");
- Stringval=attr.getInputValue().toString();
- introws=Integer.parseInt(val);
- this.setTotalRows(rows);
- doubleval1=((double)this.getTotalRows()/this.getRowsPerPage());
- inttotalPagesCal=(int)Math.ceil(val1);
- this.setTotalPages((totalPagesCal!=0)?totalPagesCal:1);
- }
- publicvoidfirstActionListener(ActionEventactionEvent){
- this.setCurrentPage(1);
- this.setFirstRow(0);
- }
- publicvoidpreviousActionListener(ActionEventactionEvent){
- this.setCurrentPage(this.getCurrentPage()-1);
- this.setFirstRow(this.getFirstRow()-this.getRowsPerPage());
- }
- publicvoidnextActionListener(ActionEventactionEvent){
- this.setCurrentPage(this.getCurrentPage()+1);
- this.setFirstRow(this.getFirstRow()+this.getRowsPerPage());
- }
- publicvoidlastActionListener(ActionEventactionEvent){
- this.setCurrentPage(this.getTotalPages());
- this.setFirstRow(this.getTotalRows()-
- ((this.getTotalRows()%this.getRowsPerPage()!=0)?this.getTotalRows()%
- this.getRowsPerPage():this.getRowsPerPage()));
- }
- publicbooleanisBeforeDisabled(){
- returnthis.getFirstRow()==0;
- }
- publicbooleanisAfterDisabled(){
- returnthis.getFirstRow()>=this.getTotalRows()-this.getRowsPerPage();
- }
- publicvoidsetFirstRow(intfirstRow){
- this.firstRow=firstRow;
- }
- publicintgetFirstRow(){
- returnfirstRow;
- }
- publicvoidsetRowsPerPage(introwsPerPage){
- this.rowsPerPage=rowsPerPage;
- }
- publicintgetRowsPerPage(){
- returnrowsPerPage;
- }
- publicvoidsetTotalRows(inttotalRows){
- this.totalRows=totalRows;
- }
- publicintgetTotalRows(){
- returntotalRows;
- }
- publicvoidsetTotalPages(inttotalPages){
- this.totalPages=totalPages;
- }
- publicintgetTotalPages(){
- returntotalPages;
- }
- publicvoidsetCurrentPage(intcurrentPage){
- this.currentPage=currentPage;
- }
- publicintgetCurrentPage(){
- returncurrentPage;
- }
4,打开CustomPagination页面的数据绑定
1)创建Action绑定
2)在variableIterator标签下添加下面代码
- <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部分
- <af:panelGroupLayoutid="pgl9"layout="horizontal">
- <af:spacerwidth="10"height="10"id="s9"/>
- <af:panelGroupLayoutid="pgl10"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="EmployeedId"id="ot1"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s7"/>
- <af:panelGroupLayoutid="pgl7"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="FirstName"id="ot6"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s10"/>
- <af:panelGroupLayoutid="pgl11"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="LastName"id="ot4"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s11"/>
- <af:panelGroupLayoutid="pgl12"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="Email"id="ot7"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s12"/>
- <af:panelGroupLayoutid="pgl15"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="Salary"id="ot10"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- </af:panelGroupLayout>
2)表的数据
- <af:iteratorid="i1"value="#{bindings.result.collectionModel}"var="row">
- <af:panelGroupLayoutid="pgl2"layout="horizontal">
- <af:spacerwidth="10"height="10"id="s3"/>
- <af:panelGroupLayoutid="pgl3"layout="horizontal"inlineStyle="width:75px;">
- <af:outputTextvalue="#{row.employeeId}"id="ot8"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s13"/>
- <af:panelGroupLayoutid="pgl13"layout="horizontal"inlineStyle="width:75px;">
- <af:outputTextvalue="#{row.firstName}"id="ot11"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s4"/>
- <af:panelGroupLayoutid="pgl4"layout="horizontal"inlineStyle="width:75px;">
- <af:outputTextvalue="#{row.lastName}"id="ot9"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s6"/>
- <af:panelGroupLayoutid="pgl5"layout="horizontal"inlineStyle="width:75px;">
- <af:outputTextvalue="#{row.email}"id="ot2"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s8"/>
- <af:panelGroupLayoutid="pgl8"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="#{row.salary}"id="ot3"/>
- </af:panelGroupLayout>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s1"/>
- </af:iterator>
3)控制按钮
- <af:panelGroupLayoutid="pgl6">
- <af:commandButtontext="First"id="cb1"
- actionListener="#{CustomPagination.firstActionListener}"
- partialTriggers="i1"disabled="#{CustomPagination.beforeDisabled}"/>
- <af:commandButtontext="Prev"id="cb2"
- actionListener="#{CustomPagination.previousActionListener}"
- partialTriggers="i1"disabled="#{CustomPagination.beforeDisabled}"/>
- <af:commandButtontext="Next"id="cb3"
- actionListener="#{CustomPagination.nextActionListener}"
- partialTriggers="i1"disabled="#{CustomPagination.afterDisabled}"/>
- <af:commandButtontext="Last"id="cb4"
- actionListener="#{CustomPagination.lastActionListener}"
- partialTriggers="i1"disabled="#{CustomPagination.afterDisabled}"/>
- </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,代码片段如下:
- publicList<Employees>employeesByLimit(intfirstRow,intmaxRow){
- StringqueryString="select*fromEmployeesorderbyemployee_idASC";
- returnem.createNativeQuery(queryString,
- Employees.class).setMaxResults(maxRow).setFirstResult(firstRow).getResultList();
- }
- /**
- *Returnstotalamountofrowsintable.
- *@returnTotalamountofrowsintable.
- */
- publicintemployeesTotalRows(){
- StringqueryString="select*fromEmployeesorderbyemployee_idASC";
- Queryquery=em.createNativeQuery(queryString);
- Listresults=query.getResultList();
- returnresults.size();
- }
2,在ViewController层创建JSPX页面CustomPagination.jspx
3,创建页面对应的sessionScope级别的managed bean
代码片段:
- privateintfirstRow=0;
- privateintrowsPerPage=10;
- privateinttotalRows;
- privateinttotalPages;
- privateintcurrentPage=1;
- publicCustomPagination(){
- this.loadList();
- }
- publicvoidloadList(){
- /**
- *Returnstotalamountofrowsintable.
- *@returnTotalamountofrowsintable.
- */
- BindingContainerbindings=BindingContext.getCurrent().getCurrentBindingsEntry();
- AttributeBindingattr=(AttributeBinding)bindings.getControlBinding("EmployeesTotalRowCount");
- Stringval=attr.getInputValue().toString();
- introws=Integer.parseInt(val);
- this.setTotalRows(rows);
- doubleval1=((double)this.getTotalRows()/this.getRowsPerPage());
- inttotalPagesCal=(int)Math.ceil(val1);
- this.setTotalPages((totalPagesCal!=0)?totalPagesCal:1);
- }
- publicvoidfirstActionListener(ActionEventactionEvent){
- this.setCurrentPage(1);
- this.setFirstRow(0);
- }
- publicvoidpreviousActionListener(ActionEventactionEvent){
- this.setCurrentPage(this.getCurrentPage()-1);
- this.setFirstRow(this.getFirstRow()-this.getRowsPerPage());
- }
- publicvoidnextActionListener(ActionEventactionEvent){
- this.setCurrentPage(this.getCurrentPage()+1);
- this.setFirstRow(this.getFirstRow()+this.getRowsPerPage());
- }
- publicvoidlastActionListener(ActionEventactionEvent){
- this.setCurrentPage(this.getTotalPages());
- this.setFirstRow(this.getTotalRows()-
- ((this.getTotalRows()%this.getRowsPerPage()!=0)?this.getTotalRows()%
- this.getRowsPerPage():this.getRowsPerPage()));
- }
- publicbooleanisBeforeDisabled(){
- returnthis.getFirstRow()==0;
- }
- publicbooleanisAfterDisabled(){
- returnthis.getFirstRow()>=this.getTotalRows()-this.getRowsPerPage();
- }
- publicvoidsetFirstRow(intfirstRow){
- this.firstRow=firstRow;
- }
- publicintgetFirstRow(){
- returnfirstRow;
- }
- publicvoidsetRowsPerPage(introwsPerPage){
- this.rowsPerPage=rowsPerPage;
- }
- publicintgetRowsPerPage(){
- returnrowsPerPage;
- }
- publicvoidsetTotalRows(inttotalRows){
- this.totalRows=totalRows;
- }
- publicintgetTotalRows(){
- returntotalRows;
- }
- publicvoidsetTotalPages(inttotalPages){
- this.totalPages=totalPages;
- }
- publicintgetTotalPages(){
- returntotalPages;
- }
- publicvoidsetCurrentPage(intcurrentPage){
- this.currentPage=currentPage;
- }
- publicintgetCurrentPage(){
- returncurrentPage;
- }
4,打开CustomPagination页面的数据绑定
1)创建Action绑定
2)在variableIterator标签下添加下面代码
- <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部分
- <af:panelGroupLayoutid="pgl9"layout="horizontal">
- <af:spacerwidth="10"height="10"id="s9"/>
- <af:panelGroupLayoutid="pgl10"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="EmployeedId"id="ot1"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s7"/>
- <af:panelGroupLayoutid="pgl7"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="FirstName"id="ot6"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s10"/>
- <af:panelGroupLayoutid="pgl11"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="LastName"id="ot4"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s11"/>
- <af:panelGroupLayoutid="pgl12"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="Email"id="ot7"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s12"/>
- <af:panelGroupLayoutid="pgl15"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="Salary"id="ot10"inlineStyle="font-weight:bold;"/>
- </af:panelGroupLayout>
- </af:panelGroupLayout>
2)表的数据
- <af:iteratorid="i1"value="#{bindings.result.collectionModel}"var="row">
- <af:panelGroupLayoutid="pgl2"layout="horizontal">
- <af:spacerwidth="10"height="10"id="s3"/>
- <af:panelGroupLayoutid="pgl3"layout="horizontal"inlineStyle="width:75px;">
- <af:outputTextvalue="#{row.employeeId}"id="ot8"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s13"/>
- <af:panelGroupLayoutid="pgl13"layout="horizontal"inlineStyle="width:75px;">
- <af:outputTextvalue="#{row.firstName}"id="ot11"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s4"/>
- <af:panelGroupLayoutid="pgl4"layout="horizontal"inlineStyle="width:75px;">
- <af:outputTextvalue="#{row.lastName}"id="ot9"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s6"/>
- <af:panelGroupLayoutid="pgl5"layout="horizontal"inlineStyle="width:75px;">
- <af:outputTextvalue="#{row.email}"id="ot2"/>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s8"/>
- <af:panelGroupLayoutid="pgl8"inlineStyle="width:75px;"layout="horizontal">
- <af:outputTextvalue="#{row.salary}"id="ot3"/>
- </af:panelGroupLayout>
- </af:panelGroupLayout>
- <af:spacerwidth="10"height="10"id="s1"/>
- </af:iterator>
3)控制按钮
- <af:panelGroupLayoutid="pgl6">
- <af:commandButtontext="First"id="cb1"
- actionListener="#{CustomPagination.firstActionListener}"
- partialTriggers="i1"disabled="#{CustomPagination.beforeDisabled}"/>
- <af:commandButtontext="Prev"id="cb2"
- actionListener="#{CustomPagination.previousActionListener}"
- partialTriggers="i1"disabled="#{CustomPagination.beforeDisabled}"/>
- <af:commandButtontext="Next"id="cb3"
- actionListener="#{CustomPagination.nextActionListener}"
- partialTriggers="i1"disabled="#{CustomPagination.afterDisabled}"/>
- <af:commandButtontext="Last"id="cb4"
- actionListener="#{CustomPagination.lastActionListener}"
- partialTriggers="i1"disabled="#{CustomPagination.afterDisabled}"/>
- </af:panelGroupLayout>
6,运行代码
首页:
第三页:
尾页: