JSF2.0 分页代码的实现
程序员文章站
2024-03-12 19:36:38
...
JSF2中DataTable的实现并没有提供分页实现,相比于JSF1.2中可用的richfaces等实现中的分页来说,JSF2可用的其它实现包并不多。参考网上一位同学的代码,并修改它的代码的bug,形成了这个新的代码。
界面代码:
后台bean:
界面代码:
<h:form>
<h:dataTable id="dt1" value="#{testPaginate.list}" binding="#{testPaginate.htmlDataTable}" border="1" var="item" rows="4" width="100%" headerClass="datagrid-header" >
<h:column>
<f:facet name="choose">
<h:outputText value="chosse" />
</f:facet>
//这里实现了多选功能或者单选功能的实现
<h:selectBooleanCheckbox >
<f:ajax listener="#{testPaginate.ajaxSelect}" execute="@this"></f:ajax>
</h:selectBooleanCheckbox>
<h:commandButton action="#{testPaginate.select}"></h:commandButton>
<h:outputText value="#{item.name}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="name" />
</f:facet>
<h:outputText value="#{item.name}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="user"/>
</f:facet>
<h:outputText value="#{item.user}"></h:outputText>
</h:column>
</h:dataTable>
<h:outputText value="total:#{testPaginate.itemCount} "/>
<h:outputText value="per page:#{testPaginate.pageSize} "/>
<h:outputText value="total page:#{testPaginate.pageCount} "/>
<h:outputText value="current:#{testPaginate.pageIndex} "/>
//本来没有加ajax用来分页。后来发现这只对分页时第一页的selectonebooleancheckbox
//起作用。后来发现,如果不加render代码在commanbutton上面的话,使得提交后的界面的
//界面找不到majarra(firebug提示)。研究了半天(半碗水的水平,只能摸索着做了,呵
//呵),才解决
<f:ajax execute="@form" render="@form">
<h:commandButton value="firstpage" action="#{testPaginate.firstAction}" disabled="#{testPaginate.pageIndex == 1 || empty testPaginate.list}"/>
<h:commandButton value="prevpage" action="#{testPaginate.prevAction}" disabled="#{testPaginate.pageIndex == 1 || empty testPaginate.list}"/>
<h:commandButton value="nextpage" action="#{testPaginate.nextAction}" disabled="#{testPaginate.pageIndex == testPaginate.pageCount || empty testPaginate.list}"/>
<h:commandButton value="lastpage" action="#{testPaginate.lastAction}" disabled="#{testPaginate.pageIndex == testPaginate.pageCount || empty testPaginate.list}"/>
<h:commandButton value="load" action="#{testPaginate.doSomething}"/>
</f:ajax>
</h:form>
后台bean:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author liujg
*/
@ManagedBean
@SessionScoped
public class TestPaginate {
private HtmlDataTable htmlDataTable=new HtmlDataTable();
public HtmlDataTable getHtmlDataTable() {
return htmlDataTable;
}
public void setHtmlDataTable(HtmlDataTable htmlDataTable) {
this.htmlDataTable = htmlDataTable;
}
public ArrayList<User> getList() {
if(list.size()<1)
for(int i=0;i<10;i++){
User user=new User();
user.setName("name"+i);
user.setUser("user"+i);
list.add(user);
}
return list;
}
public void setList(ArrayList<User> list) {
this.list = list;
}
public ArrayList<User> selected=new ArrayList<User>();
public ArrayList<User> getSelected() {
return selected;
}
public void setSelected(ArrayList<User> selected) {
this.selected = selected;
}
public void select(){
int index=htmlDataTable.getRowIndex();
User selectedUser=(User)htmlDataTable.getRowData();
System.out.println("you choose "+index+" record ,and User is"+selectedUser.getName()+":::"+selectedUser.getUser());
}
//ajaxSelect方法实现了多选功能。
public void ajaxSelect(AjaxBehaviorEvent event){
HtmlSelectBooleanCheckbox b = (HtmlSelectBooleanCheckbox)event.getComponent();
HtmlSelectBooleanCheckbox def = (HtmlSelectBooleanCheckbox)event.getSource();
System.out.println(b.isSelected());
int index=htmlDataTable.getRowIndex();
User selectedUser=(User)htmlDataTable.getRowData();
System.out.println("you choose ajax"+index+" record ,and User is"+selectedUser.getName()+":::"+selectedUser.getUser());
selected.add(selectedUser);
}
private ArrayList<User> list=new ArrayList<User>();
public int getItemCount() {
return htmlDataTable.getRowCount();
}
public int getPageCount() {
double pageDouble = (htmlDataTable.getRowCount() + htmlDataTable.getRows() - 1) / htmlDataTable.getRows();
int pageCount = (int) Math.ceil(pageDouble);
return pageCount;
}
public int getPageSize() {
return htmlDataTable.getRows();
}
public int getPageIndex() {
if(htmlDataTable.getFirst()==0){
return 1;
}
return htmlDataTable.getFirst()/htmlDataTable.getRows()+1;
}
public void doSomething(){
htmlDataTable.setFirst(htmlDataTable.getFirst() + htmlDataTable.getRows());
}
public void nextAction() {
htmlDataTable.setFirst(htmlDataTable.getFirst() + htmlDataTable.getRows());
}
public void lastAction() {
htmlDataTable.setFirst(htmlDataTable.getRowCount()/htmlDataTable.getRows()*htmlDataTable.getRows());
}
public void firstAction() {
htmlDataTable.setFirst(0);
}
public void prevAction() {
htmlDataTable.setFirst(htmlDataTable.getFirst() - htmlDataTable.getRows());
}
}
User类:
下一篇: 举例讲解Java中final关键字的用法