美文网首页
闲来无事撸个pagination

闲来无事撸个pagination

作者: 天海相接 | 来源:发表于2018-11-24 16:32 被阅读23次
/**
 * 依赖jquery,bootstrap3
 */
(function($){
    var defaultConfigure = {
        total: 0,
        current: 1,
        pageSize:10,
        totalpage:1,
        shownumber:8,
        pageSizeOption:[10,20,30,40,50]
    }
    $.fn.extend({
        pagination: function(configure,onchange) {
            var $this = $(this),
            newConfigure = $.extend(true,{},defaultConfigure,configure),
            totalpage = newConfigure.total === 0 ? 1 : Math.ceil(newConfigure.total/newConfigure.pageSize),
            con = $('<div style="white-space:nowrap; height:32px; line-height: 32px"></div>'),
            psoCon = $('<div class="text-multe pull-left"></div>'),
            pso_total = $('<div style="display:inline-block;margin-right:15px"></div>'),
            pso_select = $('<select style="background-color: white;height:32px"></select>'),
            pnCon = $('<div class="text-right"></div>'),
            pn_pn = $('<ul class="pagination" style="margin:0;margin-right:5px"></ul>'),
            pn_sel = $('<div style="display:inline-block;" class="pull-right"></div>'),
            pn_sel_btn = $('<button style="display:inline-block;" class="btn btn-default btn-sm">跳转</button>'),
            pn_sel_in = $('<input style="width:40px;display:inline-block" class="form-control input-sm " type="text" placeholder="">');
            
            newConfigure.totalpage = totalpage;
            $this.configure = newConfigure;
            
            con.append(psoCon.append(pso_total).append(pso_select));
            con.append(pnCon.append(pn_pn).append(pn_sel.append(pn_sel_btn).append($('<span>&nbsp;至&nbsp;</span>')).append(pn_sel_in).append($('<span>&nbsp;页&nbsp;</span>'))));
            $this.append(con);

            pn_sel_btn.on('click',function(){
                $this.jumpto(parseInt(pn_sel_in.val()))
            });
            pso_select.on('change',function(e){
                var c = $this.configure;
                c.pageSize = parseInt(e.target.value);
                c.totalpage = Math.ceil(c.total / c.pageSize);
                $this.refresh();
                if(onchange){
                    onchange({
                        totalpage: c.totalpage,
                        current: c.current,
                        pageSize: c.pageSize
                    });
                }
            });

            $this.createDetail = function(){
                var c = $this.configure;
                pso_total.text('总共'+c.total+'条,'+(c.total === 0 ? 1 : Math.ceil(c.total/c.pageSize))+'页');
                for(var i in c.pageSizeOption){
                    pso_select.append($('<option value=\"'+c.pageSizeOption[i]+'\">'+c.pageSizeOption[i]+'条每页</option>'))
                };
            }
            $this.create_pn_pn_child = function(){
                var c = $this.configure;
                pn_pn.empty();

                var prew = $('<li><a href="#">&laquo;</a></li>');
                prew.on('click',$this.prew);
                pn_pn.append(prew);

                var totalpage = c.totalpage;
                if(totalpage > c.shownumber){
                    var start = c.current - Math.floor(c.shownumber/2);
                    start = (start + c.shownumber - 1) > totalpage ? totalpage - c.shownumber + 1 : start;
                    start = start <= 0 ? 1 : start;
                    for(var i=start;i<c.shownumber + start;i++){
                        var li = $('<li '+(c.current === i ? 'class=\"active\"' : '') +'><a href="#">'+i+'</a></li>');
                        li.on('click',{value:i},function(e){
                            $this.jumpto(e.data.value)
                        });
                        pn_pn.append(li);
                    }
                }
                else{
                    for(var i=1; i<=totalpage; i++){
                        var li = $('<li '+(c.current === i ? 'class=\"active\"' : '') +'><a href="#">'+i+'</a></li>');
                        li.on('click',{value:i},function(e){
                            $this.jumpto(e.data.value)
                        });
                        pn_pn.append(li);
                    }
                }
                var next = $('<li><a href="#">&raquo;</a></li>');
                next.on('click',function(){
                    $this.next();
                });
                pn_pn.append(next);
            }
            $this.createDetail();
            $this.create_pn_pn_child();
            
            //跳转至某页
            $this.prew = function(){
                if($this.configure.current === 1)return;
                $this.jumpto($this.configure.current-1)
            }
            $this.jumpto = function(number){
                if(number<1 || number>$this.configure.totalpage)throw new Error('您选择的野马超出范围!');
                $this.configure.current = number;
                $this.create_pn_pn_child();
                if(onchange){
                    onchange({
                        totalpage: $this.configure.totalpage,
                        current: $this.configure.current,
                        pageSize: $this.configure.pageSize
                    });
                }
            }
            $this.next = function(){
                var c = $this.configure;
                var totalpage = c.totalpage;
                if(c.current === totalpage)return;
                $this.jumpto(c.current+1)
            }
            $this.refresh = function(configure){
                var $nc = configure || {},
                newConfigure = $.extend(true,{},$this.configure,$nc),
                totalpage = newConfigure.total === 0 ? 1 : Math.ceil(newConfigure.total/newConfigure.pageSize);

                newConfigure.totalpage = totalpage;
                $this.configure = newConfigure;

                var c = $this.configure;
                c.current = c.current > c.totalpage ? c.totalpage : c.current;
                $this.jumpto(c.current)
            }
            return $this
        }
    });
})(jQuery)

使用方法

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>

<head>
    <title>pagination</title>
    <link rel="stylesheet" type="text/css" href="/tools/bootstrap-3.3.7/css/bootstrap.min.css" />
</head>

<body>

    <div id="p">
    </div>
    <script type="text/javascript" src="/tools/jquery.min.js"></script>
    <script type="text/javascript" src="/tools/bootstrap-3.3.7/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/tools/CGGPagenation.js"></script>
    <script type="text/javascript">
        var $p = $('#p')
        .pagination(
            {total: 800,
            current: 3,
            pageSize:10},
            function(pagination){
                console.log(pagination)
            }
        );
        window.setTimeout(function(){
           $p.refresh(
            {total: 100,
            current: 3,
            pageSize:50}
           ) 
        },3000)
    </script>
</body>
<html>

相关文章

  • 闲来无事撸个pagination

    使用方法

  • jdk 动态代理源码分析

    闲来无事,撸撸源码 食用方法 直接看代码吧。。 运行了InvocationHandlerImpl中的main方法我...

  • Android随笔-自定义随机数显示

    闲来无事,撸了一个自定义view随机数,也算是学习啦

  • swiper

    点击分页器切换图片pagination: {el: ".swiper-pagination",clickable ...

  • 闲来无事可撸猫

    风卷江湖雨暗村,四山声作海涛翻。溪柴火软蛮毡暖,我与狸奴不出门。 陆游的这首《十一月四日风雨大作》,很多人只知...

  • 闲来无事多撸猫

    家有猫,2只 皮蛋,1岁半。性别,母 京东,4岁多。性别,公 京东喵同学,跟刘强东的商业帝国没有一点关系。为啥叫京...

  • 撸个Flutter个人页小demo

    最近闲来无事,撸个小demo,主要是对Flutter框架Dart语言的基础使用 Demo 连接:https://g...

  • django分页

    方法一:使用pure_pagination进行分页 1. pure_pagination介绍 pure_pagin...

  • Pagination

    Django提供了一些类来帮助您管理分页数据 - 即分布在多个页面上的数据,带有“上一页/下一页”链接。 REST...

  • 撸猫真好啊

    参加朋友婚礼,下午闲来无事,去十几公里以外的万达广场,本想去猫步咖啡撸撸猫,竟然关门。只能随便找了个咖啡厅去坐坐...

网友评论

      本文标题:闲来无事撸个pagination

      本文链接:https://www.haomeiwen.com/subject/gpryqqtx.html