javascript分页代码实例分享(js分页)


Posted in Javascript onDecember 13, 2013

调用:

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中window.location.href,location.href,parent.location.href,top.location.href的用法与区别
Oct 18 Javascript
javascript定义变量时加var与不加var的区别
Dec 22 Javascript
vue自定义指令实现v-tap插件
Nov 03 Javascript
微信小程序开发之toast提示插件使用示例
Jun 08 Javascript
React如何避免重渲染
Apr 10 Javascript
小程序组件之仿微信通讯录的实现代码
Sep 12 Javascript
JS使用canvas中的measureText方法测量字体宽度示例
Feb 02 Javascript
jQuery ajax仿Google自动提示SearchSuggess功能示例
Mar 28 jQuery
jQuery设置下拉框显示与隐藏效果的方法分析
Sep 15 jQuery
JavaScript如何实现图片处理与合成
May 29 Javascript
react中hook介绍以及使用教程
Dec 11 Javascript
前端JS获取URL参数的4种方法总结
Apr 05 Javascript
jquery 操作iframe的几种方法总结
Dec 13 #Javascript
异步动态加载JS并运行(示例代码)
Dec 13 #Javascript
JQuery中$.ajax()方法参数详解及应用
Dec 12 #Javascript
js setTimeout()函数介绍及应用以倒计时为例
Dec 12 #Javascript
jquery如何获取复选框的值
Dec 12 #Javascript
Jquery创建一个层当鼠标移动到层上面不消失效果
Dec 12 #Javascript
jQuery的each终止或跳过示例代码
Dec 12 #Javascript
You might like
将CMYK颜色值和RGB颜色相互转换的PHP代码
2014/07/28 PHP
thinkphp视图模型查询提示ERR: 1146:Table 'db.pr_order_view' doesn't exist的解决方法
2014/10/30 PHP
jQuery 性能优化指南(2)
2009/05/21 Javascript
那些年,我还在学习jquery 学习笔记
2012/03/05 Javascript
JSON简介以及用法汇总
2016/02/21 Javascript
jQuery Ajax和getJSON获取后台普通json数据和层级json数据用法分析
2016/06/08 Javascript
javascript 解决浏览器不支持的问题
2016/09/24 Javascript
jQuery的 $.ajax防止重复提交的两种方法(推荐)
2016/10/14 Javascript
js微信支付实现代码
2016/12/22 Javascript
JavaScript实现简单的四则运算计算器完整实例
2017/04/28 Javascript
node.js实现微信JS-API封装接口的示例代码
2017/09/06 Javascript
vuejs选中当前样式active的实例
2018/08/22 Javascript
swiper在vue项目中loop循环轮播失效的解决方法
2018/09/15 Javascript
通过图带你深入了解vue的响应式原理
2019/06/21 Javascript
微信小程序文章详情功能完整实例
2020/06/03 Javascript
[01:09]2014DOTA2国际邀请赛 TI4西雅图DOTA2 中国美女coser加油助威
2014/07/20 DOTA
[02:31]2018年度DOTA2最具人气选手-完美盛典
2018/12/16 DOTA
python实现爬虫下载漫画示例
2014/02/16 Python
Python使用xlrd模块操作Excel数据导入的方法
2015/05/26 Python
Python中的defaultdict与__missing__()使用介绍
2018/02/03 Python
Python实现XML文件解析的示例代码
2018/02/05 Python
Python接口自动化判断元素原理解析
2020/02/24 Python
后端开发使用pycharm的技巧(推荐)
2020/03/27 Python
Python实现定时监测网站运行状态的示例代码
2020/09/30 Python
html5组织文档结构_动力节点Java学院整理
2017/07/11 HTML / CSS
ET Mall东森购物网:东森严选
2017/03/06 全球购物
Clarins娇韵诗英国官网:来自法国的天然护肤品牌
2017/04/18 全球购物
留学生如何写好自荐信
2013/12/27 职场文书
教育孩子心得体会
2014/01/01 职场文书
旷课检讨书2000字
2014/01/14 职场文书
2014年前台个人工作总结
2014/11/14 职场文书
《小小的船》教学反思
2016/02/18 职场文书
Python数据分析之pandas函数详解
2021/04/21 Python
解决golang 关于全局变量的坑
2021/05/06 Golang
mysql中关键词exists的用法实例详解
2022/06/10 MySQL
Nginx文件已经存在全局反向代理问题排查记录
2022/07/15 Servers