javascript分页代码实例分享(js分页)
时间:2023-10-11 10:00:57
调用:
var pageChange = function (index) {
var html = pager("divid", index, 5, 1000, pageChange, { showGoTo: false, showFirst: false });
}
实现:
pager = function (divPager, pageIndex, pageSize, totalCount, pageChange, opt) {
var theOpt = {
barSize: 5, //分页条显示的页码数
barTemplate: "{bar} 共{totalPage}页{totalCount}条 {goto}", //显示模板
autoHide: true, //是否自动隐藏
showFirst: true, //在totalPage>barSize时是自动否显示第一页链接
showLast: true, //在totalPage>barSize时是自动否显示最后一页链接
showGoTo: true, //是否显示GoTo
autoHideGoTo: true //如果太少是否自动隐藏GoTo
};
if (opt) {
if (opt.barSize)
theOpt.barSize = opt.barSize;
if (opt.barTemplate)
theOpt.barTemplate = opt.barTemplate;
if (opt.autoHide == false)
theOpt.autoHide = false;
if (opt.showFirst == false)
theOpt.showFirst = false;
if (opt.showLast = false)
theOpt.showLast = false;
if (opt.showGoTo == false)
theOpt.showGoTo = false;
if (opt.autoHideGoTo == false)
theOpt.autoHideGoTo = false;
}
var handles = window.myPagerChanges = (function (x) { return x; } (window.myPagerChanges || {}));
if (!myPagerChanges[divPager]) myPagerChanges[divPager] = pageChange;
var startPage = 0; //分页条起始页
var endPage = 0; //分页条终止页
var showFirst = true;
var showLast = true;
if (isNaN(pageIndex)) {
pageIndex = 1;
}
pageIndex = parseInt(pageIndex);
if (pageIndex <= 0)
pageIndex = 1;
if (pageIndex * pageSize > totalCount) {
pageIndex = Math.ceil(totalCount / pageSize);
}
if (totalCount == 0) { //如果没数据
document.getElementById(divPager).innerHTML = "";
return "";
}
var totalPage = Math.ceil(totalCount / pageSize);
if (theOpt.autoHide && totalCount <= pageSize) { //自动隐藏
document.getElementById(divPager).innerHTML = "";
return "";
}
if (totalPage <= theOpt.barSize) {
startPage = 1;
endPage = this.totalPage;
theOpt.showLast = theOpt.showFirst = false;
}
else {
if (pageIndex <= Math.ceil(theOpt.barSize / 2)) { //最前几页时
startPage = 1;
endPage = theOpt.barSize;
theOpt.showFirst = false;
}
else if (pageIndex > (totalPage - theOpt.barSize / 2)) { //最后几页时
startPage = totalPage - theOpt.barSize + 1;
endPage = totalPage;
theOpt.showLast = false;
}
else { //中间的页时
startPage = pageIndex - Math.ceil(theOpt.barSize / 2) + 1;
endPage = pageIndex + Math.floor(theOpt.barSize / 2);
}
if (totalPage <= (theOpt.barSize * 1.5)) {
theOpt.showLast = theOpt.showFirst = false;
}
}
function _getLink(index, txt) {
if (!txt) txt = index;
return "<a href='javascript:;' style='margin: 2px 5px;border: 1px solid #6d8cad;color: #0269BA;padding: 2px 5px;text-decoration: none;' onclick='myPagerChanges[\"" + divPager + "\"](" + index + ")'>" + txt + "</a>";
}
var barHtml = ""; //分页条
barHtml += pageIndex == 1 ? "" : _getLink(pageIndex - 1, "上一页");
if (theOpt.showFirst) {
barHtml += _getLink(1) + "<span>...</span>";
}
for (var index = startPage; index <= endPage; index++) {
if (index == pageIndex) {
barHtml += "<span style='color:red;font-weight:blod; '>" + index + "</span>";
}
else {
barHtml += _getLink(index);
}
}
if (theOpt.showLast) {
barHtml += "<span>...</span>" + _getLink(totalPage);
}
barHtml += pageIndex == totalPage ? "" : _getLink(pageIndex + 1, "下一页");
var gotoHtml = ""; //goto框及按钮
if (theOpt.showGoTo && theOpt.barTemplate.indexOf("{goto}") > 0) {
if ((theOpt.autoHideGoTo && totalPage > 15) || theOpt.autoHideGoTo == false) {
var txtid = divPager + "_goIndex";
var indexVal = "document.getElementById(\"" + txtid + "\").value";
gotoHtml += "<input type='text' onkeypress='if(event.keyCode==13){myPagerChanges[\"" + divPager + "\"](" + indexVal + ")}' id='" + txtid + "' value=" + pageIndex + " style='width:30px'>";
gotoHtml += " <input type='button' class='page_bg' value='go' onclick='myPagerChanges[\"" + divPager + "\"](" + indexVal + ")'>";
}
}
//替换模板
var pagerHtml = theOpt.barTemplate.replace("{bar}", barHtml)
.replace("{totalCount}", totalCount)
.replace("{pageIndex}", pageIndex)
.replace("{totalPage}", totalPage)
.replace("{goto}", gotoHtml);
document.getElementById(divPager).innerHTML = pagerHtml;
return pagerHtml;
};
标签:javascript分页代码,js分页
0
投稿
猜你喜欢
Flask模板引擎Jinja2使用实例
2022-01-19 11:58:39
Oracle 8i字符集乱码问题析及其解决办法
2010-07-26 13:29:00
Python3.10接入ChatGPT实现逐句回答流式返回
2022-03-04 04:45:30
centos7利用yum安装mysql 8.0.12
2024-01-26 08:09:45
PHP file_get_contents 函数超时的几种解决方法
2024-06-05 09:35:53
sql 语句插入结果为select和值混合示例
2024-01-29 09:12:42
python 猴子补丁(monkey patch)
2022-12-25 02:15:13
JavaScript 学习 - 提高篇
2024-04-30 08:54:46
是在客户端确认还是在服务器端确认?
2010-07-14 21:05:00
Python中Django发送带图片和附件的邮件
2023-11-09 16:59:03
Python 中@lazyprop 装饰器的用法
2022-11-30 16:42:10
Python中使用__new__实现单例模式并解析
2021-10-29 23:11:22
关于Python正则表达式 findall函数问题详解
2022-10-24 22:18:43
Python使用base64模块进行二进制数据编码详解
2023-08-26 20:13:13
js事件委托和事件代理案例分享
2024-04-28 09:51:31
Python 'takes exactly 1 argument (2 given)' Python error
2022-04-19 00:26:05
python PyQt5 爬虫实现代码
2022-10-19 20:24:42
MS SQL7.0的数据迁移到MySQL上的一种方法
2008-11-01 16:59:00
Python命令行运行文件的实例方法
2023-05-10 13:57:56
使用python svm实现直接可用的手写数字识别
2023-06-09 13:57:19