/**
* 处理分页的
* @author TangZenghui
*
*/
public class Pagein {
private Integer totalRows;//数据总数 get
private Integer index;//当前页索引 set
private Integer pages;//总页数 get
private List<?> li;//数据集合 set
private Integer rowsByPage;//每页的数据数 set
/**
* 构造方法
* index:当前页索引
* li:要处理的集合
* rowsByPage 每页显示的数量
*/
public Pagein(Integer index, List<?> li, Integer rowsByPage) {
super();
this.index = index;
this.li = li;
this.rowsByPage = rowsByPage;
}
public Pagein() {
this.index = 1;
this.li = new ArrayList<Object>();
this.rowsByPage = 10;
}
/**
*
* @return list
*/
private List<?> changeList(){
List<Object> reli=new ArrayList<Object>();
this.totalRows=this.li.size();//计算总数据
if(this.rowsByPage<1){//让每页数不小于1否则为默认值(10)
this.rowsByPage=10;
}
//计算总页数
Integer b=this.totalRows/this.rowsByPage;
if(this.totalRows%this.rowsByPage!=0){
b++;
}
this.pages=b;
if(index<=1){index=1;}//让当前页最小为1
if(index>this.pages){index=pages;}
int a=(index-1)*this.rowsByPage;//获取当前页的第一条数据在集合中的索引
int c=rowsByPage*index;//获取当前页的最后一条数据的索引
if(c>totalRows){//计算当前页的总数
c=a+this.totalRows%this.rowsByPage;
}
//遍历当前页的数据到新的集合
for(int i=a;i<c;i++){
reli.add(this.li.get(i));
}
this.li=reli;
return this.li;
}
//getting and setting method
public Integer getTotalRows() {
return totalRows;
}
public void setTotalRows(Integer totalRows) {
this.totalRows = totalRows;
}
public Integer getIndex() {
return index;
}
public void setIndex(Integer index) {
this.index = index;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
public List<?> getLi() {
li=changeList();
return li;
}
public void setLi(List<?> li) {
this.li = li;
}
public Integer getRowsByPage() {
return rowsByPage;
}
public void setRowsByPage(Integer rowsByPage) {
this.rowsByPage = rowsByPage;
}
}
//--------------------------------------------------
/**
*和action 绑定 只需处理一下你查到的list就OK啦
*/
//--------jsp
<div class="pager">
<html:form action="/chance.do?op=list&tp=${param.tp}" >
共${chanceForm.pg.totalRows }条记录
每页<html:text property="pg.rowsByPage" size="1"/>条
第${chanceForm.pg.index}页/共${chanceForm.pg.pages }页
<a href="javascript:gopg(1)">第一页</a>
<a href="javascript:gopg(${chanceForm.pg.index-1})">上一页</a>
<a href="javascript:gopg(${chanceForm.pg.index+1})">下一页</a>
<a href="javascript:gopg(${chanceForm.pg.pages})">最后一页</a>
转到<html:text property="pg.index" size="1" />页
<html:button property="" style="width:30" οnclick="gopg(document.getElementById('pg.index').value)"> GO</html:button>
<script>
<%-- 分页提交 --%>
function gopg(i){
document.getElementById("pg.index").value=i;
document.forms[1].submit();
}
</script>
</html:form>
</div>
</th>
</tr>
</table>
</body>