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数组的操作push,pop,shift,unshift等方法详细介绍
Dec 28 Javascript
Web Inspector:关于在 Sublime Text 中调试Js的介绍
Apr 18 Javascript
JavaScript中的alert()函数使用技巧详解
Dec 29 Javascript
jQuery中delegate()方法用法实例
Jan 19 Javascript
jQuery中closest和parents的区别分析
May 07 Javascript
jquery中toggle函数交替使用问题
Jun 22 Javascript
jQuery实现仿腾讯视频列表分页效果的方法
Aug 07 Javascript
jQuery简单实现tab选项卡切换效果
Jun 20 Javascript
seajs模块压缩问题与解决方法实例分析
Oct 10 Javascript
了解ESlint和其相关操作小结
May 21 Javascript
通过微信公众平台获取公众号文章的方法示例
Dec 25 Javascript
vue data有值,但是页面{{}} 取不到值的解决
Nov 09 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
数据库相关问题
2006/10/09 PHP
php实现大文件断点续传下载实例代码
2019/10/01 PHP
根据分辨率不同,调用不同的css文件
2006/07/07 Javascript
JavaScript重载函数实例剖析
2016/05/13 Javascript
ReactJs实现树形结构的数据显示的组件的示例
2017/08/18 Javascript
JS实现调用本地摄像头功能示例
2018/05/18 Javascript
解决mpvue + vuex 开发微信小程序vuex辅助函数mapState、mapGetters不可用问题
2018/08/03 Javascript
React key值的作用和使用详解
2018/08/23 Javascript
Vue 组件封装 并使用 NPM 发布的教程
2018/09/30 Javascript
深入理解移动前端开发之viewport
2018/10/19 Javascript
详解@angular/cli 改变默认启动端口两种方式
2018/11/29 Javascript
vue中$refs, $emit, $on, $once, $off的使用详解
2019/05/26 Javascript
layui form表单提交后实现自动刷新
2019/10/25 Javascript
微信小程序实现点击按钮后修改颜色
2019/12/05 Javascript
微信小程序地图绘制线段并且测量(实例代码)
2020/01/02 Javascript
Antd-vue Table组件添加Click事件,实现点击某行数据教程
2020/11/17 Javascript
vue使用transition组件动画效果的实例代码
2021/01/28 Vue.js
Python functools模块学习总结
2015/05/09 Python
Python检测字符串中是否包含某字符集合中的字符
2015/05/21 Python
简单谈谈python中的多进程
2016/11/06 Python
TensorFlow如何实现反向传播
2018/02/06 Python
Python语言的变量认识及操作方法
2018/02/11 Python
用python3教你任意Html主内容提取功能
2018/11/05 Python
linux环境中没有网络怎么下载python
2019/07/07 Python
python Django的web开发实例(入门)
2019/07/31 Python
python getpass模块用法及实例详解
2019/10/07 Python
浅谈对pytroch中torch.autograd.backward的思考
2019/12/27 Python
Python 处理日期时间的Arrow库使用
2020/08/18 Python
M1芯片安装python3.9.1的实现
2021/02/02 Python
细说CSS3中box属性中的overflow-x属性和overflow-y属性值的效果
2014/07/21 HTML / CSS
德国、奥地利和瑞士最大的旅行和度假门户网站:HolidayCheck
2019/11/14 全球购物
房地产开盘策划方案
2014/02/10 职场文书
教师节横幅标语
2014/10/08 职场文书
2014年加油站站长工作总结
2014/12/23 职场文书
Nginx本地目录映射实现代码实例
2021/03/31 Servers
MySQL中varchar和char类型的区别
2021/11/17 MySQL