jsp + jquery 分页插件 (适用分页较多的情况)

效果图:




<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../resource/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript">


var currentpage_static=0;
var datanum_static=0;
var pagesize_static=0;
var maxpagesize_static=0;
//初始化参数
function  initpages(datanum,pagesize,maxpagesize,currentpage){
//求商
var  pagenum=parseInt(datanum/pagesize);
//求余
var  lastpage=(datanum%pagesize);
//总页数
if(lastpage>0){
pagenum++;
}
//公共变量赋值
currentpage_static=currentpage;
datanum_static=datanum;
pagesize_static=pagesize;
maxpagesize_static=maxpagesize;

//组件赋值
$("#pagenum").empty();
$("#datanum").empty();
$("#pagesize").empty();
$("#currentpage").empty();


$("#pagenum").append(pagenum);
$("#datanum").append(datanum);
$("#pagesize").append(pagesize);
$("#currentpage").append(currentpage);

    pagegroup(pagenum,maxpagesize,currentpage);

}
//将页码进行分组
function  pagegroup(pagenum,maxpagesize,currentpage){
    //页码总数
var pages=parseInt(pagenum/maxpagesize);
if(pagenum%maxpagesize>0)
pages++;
//当前页

var curpage=0;
if(currentpage<pagenum){
curpage=parseInt(currentpage/maxpagesize);
if(currentpage%maxpagesize>0)
curpage++;
}else{
curpage=pages;
currentpage_static=pagenum;
}

if(curpage==1){
$("#lastgroupid").attr('disabled',true);
}else{
$("#lastgroupid").attr('disabled',false);
}

if(currentpage==1){
//$("#lastpage").hide();
$("#lastpage").attr('disabled',true);

}else{
$("#lastpage").attr('disabled',false);
}
if(currentpage>=pagenum){
currentpage=pagenum;
$("#nextpage").attr('disabled',true);
}else{
$("#nextpage").attr('disabled',false);
}

//当前所在的第一条数据
var currentfirstpage=(curpage-1)*maxpagesize+1;
//当前所在的最后一条数据
var currentlastpage=0;
if(curpage==pages){
currentlastpage=pagenum;
$("#nextgroupid").attr('disabled',true);
}else{
//非尾页
  currentlastpage=curpage*maxpagesize;
$("#nextgroupid").attr('disabled',false);
}
//展示
$("#pagelist").empty();
for(var i=currentfirstpage;i<=currentlastpage;i++){
var pagehref=null;    
if(i==currentpage){
 pagehref= '<a  href="javascript:void(0)" οnclick="listbypage('+i+')"  style="color: red;">'+i+'</a>';
   }else{
    pagehref= '<a  href="javascript:void(0)" οnclick="listbypage('+i+')" style="color: black;">'+i+'</a>';
   }
$("#pagelist").append(pagehref+"    ");
}

}




//返回跳转的页码
function  jumpTopage(){
currentpage_static=parseInt($("#jumpTo").val());
initpages(datanum_static,pagesize_static,maxpagesize_static,currentpage_static);
usersubmit(currentpage_static);
}


//点击下一组方法
function   nextpagegroup(){
currentpage_static=currentpage_static+20;
initpages(datanum_static,pagesize_static,maxpagesize_static,currentpage_static);
usersubmit(currentpage_static);
}


//点击上一组方法
function   lastpagegroup(){
currentpage_static=currentpage_static-20;
initpages(datanum_static,pagesize_static,maxpagesize_static,currentpage_static);
usersubmit(currentpage_static);


}
//上一页
function  lastpage(){
currentpage_static=currentpage_static-1;
initpages(datanum_static,pagesize_static,maxpagesize_static,currentpage_static);
usersubmit(currentpage_static);
}
//下一页
function  nextpage(){
currentpage_static=currentpage_static+1;
initpages(datanum_static,pagesize_static,maxpagesize_static,currentpage_static);
usersubmit(currentpage_static);
}


//点击页码
function listbypage(page){
currentpage_static=parseInt(page);
initpages(datanum_static,pagesize_static,maxpagesize_static,currentpage_static);
usersubmit(currentpage_static);
}




/*******************用户扩展****************************/
//用户重写提交方法
/* function   usersubmit(page){
window.location.href="./chaxun.jsp?currentpage="+page; 
} */

/*

用法:举例

<%@ include file="/zfpj/pages.jsp" %>
<script>
//分页
var currentpage=1;
currentpage=(<%=request.getParameter("currentpage") %>==null)?1:<%=request.getParameter("currentpage") %>;

/*
1.数据总数
2.每页显示数据数
3.最多显示页数(用于数据量很大的情况)
4.当前页码
*/
initpages(10000,20,20,currentpage);


function   usersubmit(page){
window.location.href="./chaxun.jsp?currentpage="+page; 
}


</script>


*/



</script>
</head>
<body>
<div style="display: inline;">
<!-- 跳转页面  用于submit提交 -->
<input type="hidden" id="topagenum" name="topagenum" />
<button id="lastpage" οnclick="lastpage()"> 上一页</button>
<button id="lastgroupid" οnclick="lastpagegroup()"> << </button>
</div>
<div style="display: inline;" id="pagelist">

</div>
<div style="display: inline;">
<button id="nextgroupid" οnclick="nextpagegroup()">>></button>
<button id="nextpage"οnclick="nextpage()"> 下一页</button>
    第<div style="display: inline;" id="currentpage"></div>页    
    共<div style="display: inline;" id="pagenum"></div>页     
    共<div style="display: inline;" id="datanum"></div>条    
  每页<div style="display: inline;" id="pagesize"></div>条  
 
转到<input type="text" id="jumpTo" style="width: 30px"/> 页 
<button οnclick="jumpTopage()">跳转</button>
    </div>


</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * @(#)PageControl.java 1.00 2004-9-22 * * Copyright 2004 2004 . All rights reserved. * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.hexiang.utils; /** * PageControl, 分页控制, 可以判断总页数和是否有上下页. * * 2008-07-22 加入输出上下分页HTML代码功能 * * @author HX * @version 1.1 2008-9-22 */ public class PageBean { /** 每页显示记录数 */ private int pageCount; /** 是否有上一页 */ private boolean hasPrevPage; /** 记录总数 */ private int recordCount; /** 是否有下一页 */ private boolean hasNextPage; /**总页面数 */ private int totalPage; /** 当前页码数 */ private int currentPage; /** * 分页前的页面地址 */ private String pageUrl; /** * 输出分页 HTML 页面跳转代码, 分链接和静态文字两种. * 2008-07-22 * @return HTML 代码 */ public String getPageJumpLinkHtml() { if(StringUtil.isEmpty(pageUrl)) { return ""; } // 检查是否有参数符号, 没有就加上一个? if(pageUrl.indexOf('?') == -1) { pageUrl = pageUrl + '?'; } StringBuffer buff = new StringBuffer("<span id='pageText'>"); // 上一页翻页标记 if(currentPage > 1) { buff.append("[ <a href='" + pageUrl + "&page=" + (currentPage - 1) + "' title='转到第 " + (currentPage - 1) + " 页'>上一页</a> ] "); } else { buff.append("[ 上一页 ] "); } // 下一页翻页标记 if(currentPage < getTotalPage()) { buff.append("[ <a href='" + pageUrl + "&page=" + (currentPage + 1)+ "' title='转到第 " + (currentPage + 1) + " 页'>下一页</a> ] "); } else { buff.append("[ 下一页 ] "); } buff.append("</span>"); return buff.toString(); } /** * 输出页码信息: 第${currentPage}页/共${totalPage}页 * @return */ public String getPageCountHtml() { return "第" + currentPage + "页/共" + getTotalPage() + "页"; } /** * 输出 JavaScript 跳转函数代码 * @return */ public String getJavaScriptJumpCode() { if(StringUtil.isEmpty(pageUrl)) { return ""; } // 检查是否有参数符号, 没有就加上一个? if(pageUrl.indexOf("?") == -1) { pageUrl = pageUrl + '?'; } return "<script>" + "// 页面跳转函数\n" + "// 参数: 包含页码的表单元素,例如输入框,下拉框等\n" + "function jumpPage(input) {\n" + " // 页码相同就不做跳转\n" + " if(input.value == " + currentPage + ") {" + " return;\n" + " }" + " var newUrl = '" + pageUrl + "&page=' + input.value;\n" + " document.location = newUrl;\n" + " }\n" + " </script>"; } /** * 输出页面跳转的选择框和输入框. 示例输出: * <pre> 转到 <!-- 输出 HTML SELECT 元素, 并选当前页面编码 --> <select onchange='jumpPage(this);'> <c:forEach var="i" begin="1" end="${totalPage}"> <option value="${i}" <c:if test="${currentPage == i}"> selected </c:if> >第${i}页</option> </c:forEach> </select> 输入页码:<input type="text" value="${currentPage}" id="jumpPageBox" size="3"> <input type="button" value="跳转" onclick="jumpPage(document.getElementById('jumpPageBox'))"> </pre> * @return */ public String getPageFormJumpHtml() { String s = "转到\n" + "\t <!-- 输出 HTML SELECT 元素, 并选当前页面编码 -->\n" + " <select onchange='jumpPage(this);'>\n" + " \n"; for(int i = 1; i <= getTotalPage(); i++ ) { s += "<option value=" + i + "\n"; if(currentPage == i) { s+= " selected "; } s += "\t>第" + i + "页</option>\n"; } s+= " </select>\n" + " 输入页码:<input type=\"text\" value=\"" + currentPage + "\" id=\"jumpPageBox\" size=\"3\"> \n" + " <input type=\"button\" value=\"跳转\" onclick=\"jumpPage(document.getElementById('jumpPageBox'))\"> "; return s; } /** * 进行分页计算. */ private void calculate() { if (getPageCount() == 0) { setPageCount(1); } totalPage = (int) Math.ceil(1.0 * getRecordCount() / getPageCount()); // 总页面数 if (totalPage == 0) totalPage = 1; // Check current page range, 2006-08-03 if(currentPage > totalPage) { currentPage = totalPage; } // System.out.println("currentPage=" + currentPage); // System.out.println("maxPage=" + maxPage); // // Fixed logic error at 2004-09-25 hasNextPage = currentPage < totalPage; hasPrevPage = currentPage > 1; return; } /** * @return Returns the 最大页面数. */ public int getTotalPage() { calculate(); return totalPage; } /** * @param currentPage * The 最大页面数 to set. */ @SuppressWarnings("unused") private void setTotalPage(int maxPage) { this.totalPage = maxPage; } /** * 是否有上一页数据 */ public boolean hasPrevPage() { calculate(); return hasPrevPage; } /** * 是否有下一页数据 */ public boolean hasNextPage() { calculate(); return hasNextPage; } // Test public static void main(String[] args) { PageBean pc = new PageBean(); pc.setCurrentPage(2); pc.setPageCount(4); pc.setRecordCount(5); pc.setPageUrl("product/list.do"); System.out.println("当前页 " + pc.getCurrentPage()); System.out.println("有上一页 " + pc.hasPrevPage()); System.out.println("有下一页 " + pc.hasNextPage()); System.out.println("总页面数 " + pc.getTotalPage()); System.out.println("分页 HTML 代码 " + pc.getPageJumpLinkHtml()); } /** * @return Returns the 当前页码数. */ public int getCurrentPage() { return currentPage; } /** * 设置当前页码, 从 1 开始. * @param currentPage * The 当前页码数 to set. */ public void setCurrentPage(int currentPage) { if (currentPage <= 0) { currentPage = 1; } this.currentPage = currentPage; } /** * @return Returns the recordCount. */ public int getRecordCount() { return recordCount; } /** * @param recordCount * The recordCount to set. */ public void setRecordCount(int property1) { this.recordCount = property1; } /** * @return Returns the 每页显示记录数. */ public int getPageCount() { return pageCount; } /** * @param pageCount * The 每页显示记录数 to set. */ public void setPageCount(int pageCount) { this.pageCount = pageCount; } public String getPageUrl() { return pageUrl; } public void setPageUrl(String value) { pageUrl = value; } }
JspPageControlor分页插件使用说明 1、次插件将数据库查询和分页操作分离开,在查询的时候,不管采用什么设计模式,都必须实现 PageListener接口。 2、接口里面包含四个方法 (1)、publicListdoSelect(intrecordStart,intsizePage),参数recordStart表示从第几条 记录开始查询。参数sizePage表示一次查几条记录。返回一个list对象(该list对象可以是 一个记录集,也可以是一个POJO类对象) (2)、publicintgetCount(),查询该表的记录总数,返回一个int对象。 (3)、publicvoidclose(),放置所有关闭操作的方法。 3、调用页面,必须添加PageListener******,即实现addPageListener(Object)方法,参数为操作 数据库类的对象。 4、用getRecord()方法获取存有记录的list对象; 用getFirstPage()方法获取'首页'的标记; 用getPageUp()方法获取'上一页'的标记; 用getPageDown()方法获取'下一页'的标记; 用getLastPage()方法获取'末页'的标记; 用getPageForward()方法获取'跳转到第几页'的输入框标记; 用getCountRecord()方法获取总记录数; 用getCountPage()方法获取总页数; 用getCurrentPage()方法获取当前页数; 5、如果需要把上一页,下一页等标记换成图片,则只需调用 setPageDown('图片路径+图片名'), setPageUp('图片路径+图片名'), setFirstPage('图片路径+图片名'), setLastPage('图片路径+图片名'); 详细操作请查看帮助文档和具体实现案例; PageControlor分页插件(次插件仅支持jsp) 包含3个文件: PageControlor分页插件分页的帮助文档; 分页插件使用案例; 运行使用具体案例时,须注意以下几点: 1、运行环境为jdk(1.4以上),Tomcat(5.0以上),mysql(5.0.x); 2、运行时须改动init.properties,把数据库名,用户名和密码改称自己数据库的设置; 3、将news.sql文件导入到您的mysql服务器里面。 开发者:千里web架构实验室成员--刘捷 2007年10月29日
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值