jquery+bootstrap分页工具条插件

本文介绍了一个基于jQuery和Bootstrap的分页插件实现方法。该插件能够方便地在网页中添加分页导航栏,并支持自定义每页显示数量、当前页等参数。


插件实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1.0">-->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="../../docs-assets/ico/favicon.png">

    <title>Grid Template for Bootstrap</title>
    <!-- 最新 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/css/bootstrap.min.css">

    <!-- 可选的Bootstrap主题文件(一般不用引入) -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/css/bootstrap-theme.min.css">

    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <script>
        //定义全局变量
        var page_size = 0;//总页数
        var current_page_num = 1;//当前页码
        var page_num = 0; //分页工具条最多可以显示的页数
        var temp_current_page_method = null;
        var page_ination_ul = "<ul class='pagination' id='ination_page_ul'></ul>";
        var _front_page = "<li class='' id='front_page'><a href='javascript:void(0)' onclick='${_front_page}' id='front_Page_a'>上一页</a></li>";
        var _next_page = "<li class='' id='next_page'><a href='javascript:void(0)' id='next_Page_a' onclick='${_next_page}'>下一页</a></li>";
        var temp_page = "<li class='' id='${temp_page_li}'><a href='javascript:void(0)' onclick=${temp_page_method} id='${temp_page_a}'>${_page}</a></li>"
        //初始化
        function pageination_init(container,size,pagenum,current_page_method){
            page_size = size;
            page_num = pagenum;
            temp_current_page_method = current_page_method;
            //如果总页数小于1则不显示分页工具条
            if(size<=1){
                return;
            }
            var temp_page_size = 0;
            if(page_size<=page_num){
                temp_page_size = page_size;
            }else{
                temp_page_size = page_num;
            }
            $("#"+container).html(page_ination_ul);
            add_page_ination_li(1,temp_page_size,1);
        }

        //跳转到某一页
        function jump_to(){

        }

        function add_page_ination_li(start,end,page_num){
            //初始化上一页和下一页
            var front_page_html = _front_page;
            var next_page_html = _next_page;
            front_page_html = front_page_html.replace("${_front_page}","get_front_page()");
            next_page_html = next_page_html.replace("${_next_page}","get_next_page()");
            $("#ination_page_ul").append(front_page_html);//先append上一页
            for(var i=start;i<=end;i++){
                var temp_page_html = temp_page;
                temp_page_html = temp_page_html.replace("${_page}",i);
                temp_page_html = temp_page_html.replace("${temp_page_li}","page_li_"+i);
                temp_page_html = temp_page_html.replace("${temp_page_a}","page_a_"+i);
                temp_page_html = temp_page_html.replace("${temp_page_method}","get_current_page('"+i+"')");
                $("#ination_page_ul").append(temp_page_html);
            }
            $("#ination_page_ul").append(next_page_html);
            get_current_page(page_num);

        }

        function get_current_page(page_num){
            /*if(current_page_num == page_num){
             return;
             }*/

            current_page_num = page_num;
            $("li[id^=page_li_]").removeClass("disabled");
            $("#page_li_"+page_num).addClass("disabled");
            if(page_num==1){
                $("#front_page").addClass("disabled");
            }else{
                $("#front_page").removeClass("disabled");
            }
            if(page_num==page_size){
                $("#next_page").addClass("disabled");
            }else{
                $("#next_page").removeClass("disabled");
            }
            //触发获取数据事件
            temp_current_page_method(page_num);
        }

        function get_front_page(){
            if(current_page_num<=1){
                return;
            }
            current_page_num = Number($.trim(current_page_num));
            current_page_num = current_page_num-1;

            if($("#page_li_"+current_page_num).html()!=undefined&&$("#page_li_"+current_page_num).html()!=null){
                get_current_page(current_page_num);
                return;
            }
            var _blog = current_page_num-page_num+1;
            var temp_num = 0
            if(_blog<1){
                temp_num = 1 ;
            }else{
                temp_num = _blog;
            }
            $("#ination_page_ul").html("");
            add_page_ination_li(temp_num,current_page_num,current_page_num);
        }

        function get_next_page(){
            if(current_page_num>=page_size){
                return;
            }
            current_page_num = Number($.trim(current_page_num));
            current_page_num = current_page_num+1;
            if($("#page_li_"+current_page_num).html()!=undefined&&$("#page_li_"+current_page_num).html()!=null){
                get_current_page(current_page_num);
                return;
            }

            var _blog = current_page_num+page_num-1;
            var temp_num = 0
            if(_blog>page_size){
                temp_num =  page_size;
            }else{
                temp_num = _blog;
            }
            $("#ination_page_ul").html("");
            add_page_ination_li(current_page_num,temp_num,current_page_num);
        }

        /*test_current_page_method为获取后台第num页数据并显示到页面上的方法*/
        function test_current_page_method(num){
            //alert(num)
            $("#testdiv1").html("<p>这是第<h3 style='color:red'>"+num+"</h3>页的数据</p>");
        }
        /*你要做的就是将插件激活*/
        $(document).ready(function(){
            pageination_init("testdiv2",23,5,test_current_page_method);
        });
    </script>
</head>
<body>
     <div style=''>
      <div id="testdiv1"></div>
      <div id='testdiv2'></div>
     </div>
</body>
</html>

一.适合环境

  jquery+bootstrp构建的html页面

二.代码片段

//定义全局变量
var page_size = 0;//总页数
var current_page_num = 1;//当前页码
var page_num = 0; //分页工具条最多可以显示的页数
var temp_current_page_method = null;
var page_ination_ul = "<ul class='pagination' id='ination_page_ul'></ul>";
var _front_page = "<li class='' id='front_page'><a href='javascript:void(0)' onclick='${_front_page}' id='front_Page_a'>上一页</a></li>";
var _next_page = "<li class='' id='next_page'><a href='javascript:void(0)' id='next_Page_a' onclick='${_next_page}'>下一页</a></li>";
var temp_page = "<li class='' id='${temp_page_li}'><a href='javascript:void(0)' onclick=${temp_page_method} id='${temp_page_a}'>${_page}</a></li>"
//初始化
function pageination_init(container,size,pagenum,current_page_method){
    page_size = size;
    page_num = pagenum;
    temp_current_page_method = current_page_method;
    //如果总页数小于1则不显示分页工具条
    if(size<=1){
       return;
    }
    var temp_page_size = 0;
    if(page_size<=page_num){
        temp_page_size = page_size;
    }else{
        temp_page_size = page_num;
    }
    $("#"+container).html(page_ination_ul);
    add_page_ination_li(1,temp_page_size,1);
}

function add_page_ination_li(start,end,page_num){
    //初始化上一页和下一页
    var front_page_html = _front_page;
    var next_page_html = _next_page;
    front_page_html = front_page_html.replace("${_front_page}","get_front_page()");
    next_page_html = next_page_html.replace("${_next_page}","get_next_page()");
    $("#ination_page_ul").append(front_page_html);//先append上一页
    for(var i=start;i<=end;i++){
        var temp_page_html = temp_page;
        temp_page_html = temp_page_html.replace("${_page}",i);
        temp_page_html = temp_page_html.replace("${temp_page_li}","page_li_"+i);
        temp_page_html = temp_page_html.replace("${temp_page_a}","page_a_"+i);
        temp_page_html = temp_page_html.replace("${temp_page_method}","get_current_page('"+i+"')");
        $("#ination_page_ul").append(temp_page_html);
    }
    $("#ination_page_ul").append(next_page_html);
    get_current_page(page_num);

}

function get_current_page(page_num){
    current_page_num = page_num;
    $("li[id^=page_li_]").removeClass("disabled");
    $("#page_li_"+page_num).addClass("disabled");
    if(page_num==1){
       $("#front_page").addClass("disabled");
    }else{
        $("#front_page").removeClass("disabled");
    }
    if(page_num==page_size){
        $("#next_page").addClass("disabled");
    }else{
        $("#next_page").removeClass("disabled");
    }
    //触发获取数据事件
    temp_current_page_method(page_num);
}

function get_front_page(){
   if(current_page_num<=1){
        return;
   }
   current_page_num = Number($.trim(current_page_num));
   current_page_num = current_page_num-1;

   if($("#page_li_"+current_page_num).html()!=undefined&&$("#page_li_"+current_page_num).html()!=null){
       get_current_page(current_page_num);
       return;
   }
   var _blog = current_page_num-page_num+1;
   var temp_num = 0
   if(_blog<1){
       temp_num = 1 ;
   }else{
       temp_num = _blog;
   }
   $("#ination_page_ul").html("");
   add_page_ination_li(temp_num,current_page_num,current_page_num);
}

function get_next_page(){
    if(current_page_num>=page_size){
        return;
    }
    current_page_num = Number($.trim(current_page_num));
    current_page_num = current_page_num+1;
    if($("#page_li_"+current_page_num).html()!=undefined&&$("#page_li_"+current_page_num).html()!=null){
        get_current_page(current_page_num);
        return;
    }
    var _blog = current_page_num+page_num-1;
    var temp_num = 0
    if(_blog>page_size){
        temp_num =  page_size;
    }else{
        temp_num = _blog;
    }
    $("#ination_page_ul").html("");
    add_page_ination_li(current_page_num,temp_num,current_page_num);
}

//测试
function test_pageination(){
    pageination_init("test_pageination",23,5,test_current_page_method);
}

function test_current_page_method(num){
    //alert(num)
}
三.使用方法:

1,创建一个js文件,将上面的代码复制进去,然后在页面引入(前提是必须引入jquery和bootstrp)

2,自己再写一个js文件,用来取后台数据,首先需要一个方法获取要展示的数据的总条数,然后调用初始化方法

pageination_init(container,size,pagenum,current_page_method);
参数说明:container是包裹分页工具条的div的id(注意不要加#),size是需要展示的数据的总数目,pageunm是分页工具条最多显示的页数(比喻说你要显示页号不大于5,显示的效果就是1,2,3,4,5.第六叶开始隐藏了),current_page_method是你根据指定的页号获取后台数据的方法名字

四.刚写完没好好测试,如果使用过程有什么问题可以小纸条...

------------------------------------------------------------------------------如果你看到这里,恭喜你,现在有一个比较完美的更新提供给你---------------------------------------------------------------------

这一次,我将分页写成一个jquery插件的方式,这样你可以在同一个页面设置多个分页工具条

(function ($) {
            var pageination = function(options){
                var config={}
                config.container = options.container==undefined?this.defaults.container:options.container
                config.current_page_num = options.current_page_num==undefined?this.defaults.current_page_num:options.current_page_num
                config.page_size = options.page_size==undefined?this.defaults.page_size:options.page_size
                config.allow_size = options.allow_size==undefined?this.defaults.allow_size:options.allow_size
                config.return_method = options.return_method==undefined?this.defaults.return_method:options.return_method
                var opts=config
                render_pageination()
                function render_pageination(){
                    if(opts.page_size<1){
                        return;
                    }
                    var a=Math.floor(opts.allow_size/2),
                            o=opts.current_page_num,
                            b=o - a,
                            c=o + a,
                            d=o>a?c:opts.allow_size,
                            e=o>a?b: 1,
                            f=d>opts.page_size?opts.page_size: d,
                            g=e+opts.allow_size>opts.page_size?opts.page_size-opts.allow_size+1: e,
                            h=opts.allow_size%2==0?f:f+1
                    var start = g<1?1:g
                    var end = h
                    //拼接html
                    var ht = "<ul class='pagination'><li ><a href='#' name='to_front'>«</a></li><li ><a href='#' name='1'>第1页</a></li>"
                    for(var i=start;i<end;i++){
                        if(i==opts.current_page_num){
                            ht +="<li class='active'><a href='#' name="+i+">"+i+"</a></li>"
                        }else{
                            ht +="<li><a href='#' name="+i+">"+i+"</a></li>"
                        }
                    }

                    ht +="<li><a href='#' name='"+opts.page_size+"'>第"+opts.page_size+"页</a></li><li><a href='#' name='to_next'>»</a></li></ul>"
                    var _container = opts.container.split("&")
                    for(var i=0;i<_container.length;i++){
                        $("#"+_container[i]).html(ht)
                        $("#"+_container[i]).children("ul").children("li").children("a").bind("click",clickmethod)
                    }
                    $("#homepage_pagination").children("ul").addClass("pagination-sm")
                }

                function clickmethod(event){
                    //上一页
                    if(event.target.name=='to_front'){
                        opts.current_page_num=opts.current_page_num-1<1?1:opts.current_page_num-1
                        opts.return_method(opts.current_page_num)
                        render_pageination()
                        return;
                    }

                    //下一页
                    if(event.target.name=='to_next'){
                        opts.current_page_num=opts.current_page_num+1>opts.page_size?opts.page_size:opts.current_page_num+1
                        opts.return_method(opts.current_page_num)
                        render_pageination()
                        return;
                    }
                    opts.current_page_num = parseInt(event.target.name)
                    opts.return_method(opts.current_page_num)
                    render_pageination()
                }
            };
            pageination.prototype = {
                defaults :{
                    'container':"body",
                    'current_page_num':1,
                    'page_size' : 18,
                    'allow_size':5,
                    'return_method':""
                }
            };
            window.pageination = pageination;
        })(jQuery);

跟上面的一样,要使用它,你必须要引入jquery.min.js和bootstrap.min.css

使用方法

function tt(num){
            //alert(num)
        }

        $(function(){
            var p1 = new pageination({
                container:"homepage_pagination_top",
                page_size:19,
                allow_size:7,
                return_method:tt
            })

            var p2 = new pageination({
                container:"homepage_pagination",
                page_size:30,
                return_method:tt
            })
        })




06-22
### 得物技术栈及开发者文档分析 得物作为一家专注于潮流商品的电商平台,其技术栈和开发者文档主要围绕电商平台的核心需求展开。以下是对得物技术栈及相关开发资源的详细解析: #### 1. 技术栈概述 得物的技术栈通常会涵盖前端、后端、移动应用开发以及大数据处理等多个领域。以下是可能涉及的主要技术栈[^3]: - **前端开发**: 前端技术栈可能包括现代框架如 React 或 Vue.js,用于构建高效、响应式的用户界面。此外,还会使用 Webpack 等工具进行模块化打包和优化。 - **后端开发**: 后端技术栈可能采用 Java Spring Boot 或 Node.js,以支持高并发和分布式架构。数据库方面,MySQL 和 Redis 是常见的选择,分别用于关系型数据存储和缓存管理。 - **移动应用开发**: 得物的移动应用开发可能基于原生技术(如 Swift/Kotlin)或跨平台框架(如 Flutter)。这有助于确保移动端应用的性能和用户体验一致性。 - **大数据云计算**: 在大数据处理方面,得物可能会使用 Hadoop 或 Spark 进行数据挖掘和分析。同时,依托云服务提供商(如阿里云或腾讯云),实现弹性扩展和资源优化。 #### 2. 开发者文档分析 类似于引用中提到的 Adobe 开发者文档模板[^2],得物也可能提供一套完整的开发者文档体系,以支持内部团队协作和外部开发者接入。以下是开发者文档可能包含的内容: - **API 文档**: 提供 RESTful API 或 GraphQL 的详细说明,帮助开发者快速集成得物的功能模块,例如商品搜索、订单管理等。 - **SDK 集成指南**: 针对不同平台(如 iOS、Android 或 Web)提供 SDK 下载和集成教程,简化第三方应用的开发流程。 - **技术博客**: 分享得物在技术实践中的经验成果,例如如何优化图片加载速度、提升应用性能等。 - **开源项目**: 得物可能将部分技术成果开源,供社区开发者学习和贡献。这不仅有助于提升品牌形象,还能吸引更多优秀人才加入。 #### 3. 示例代码 以下是一个简单的示例代码,展示如何通过 RESTful API 调用得物的商品搜索功能(假设接口已存在): ```python import requests def search_items(keyword, page=1): url = "https://api.dewu.com/v1/items/search" headers = { "Authorization": "Bearer YOUR_ACCESS_TOKEN", "Content-Type": "application/json" } params = { "keyword": keyword, "page": page, "size": 10 } response = requests.get(url, headers=headers, params=params) if response.status_code == 200: return response.json() else: return {"error": "Failed to fetch data"} # 调用示例 result = search_items("Air Jordan", page=1) print(result) ``` 此代码片段展示了如何通过 Python 请求得物的 API,并获取指定关键词的商品列表。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值