JS 之 分页

主要思路

  1. 根据总条数 和 每页数量计算 总页数;
  2. 无省略页码, 如果总页数 <= 分页按钮数量, 则按实际页数输出按钮;
  3. 省略页面码在前, 如果当前页 为 最后3页, 则起始页 = 总页数 - 分页按钮数量 + 1,起始页的下一页为省略页码;
  4. 省略页面码在后, 如果当前页 为 分页按钮数量 - 2,不是最后两个按钮上, 则 倒数第二个按钮为省略页码,最后一个按钮为最后一页;
  5. 否则省略页码存在于两端, 第二个和倒数第二个按钮为省略按钮, 第一个和倒数第一个, 分别为第一页和最后一页, 当前页 位于剩余按钮的中间, 所以左右按钮分别为当前页递减递增;

调用

new Page(el,{count:8}).setPage({current:1,pageSize:10,total:100})

全文

var Page = function(el, options) {
    var defaults = {
        current: 1,   // 当前页
        total: 0,     // 总页数
        count: 8,     // 分页按钮数量 // 至少为7个按钮
        pageSize: 10  // 每页的数量,如每页10条
    };
    this.html = [];
    this.item = '<li data-index="@page"><a>@page</a></li>';  // 正常页码
    this.itemActive = '<li class="active" data-index="@page"><span>@page</span></li>';  // 当前页,激活页
    this.itemDis = '<li class="disabled more"><span>...</span></li>';     // 省略页码
    this.itemPrev = '<li data-index="prev" class="@dis"><a>«</a></li>';   // 上一页
    this.itemNext = '<li data-index="next"  class="@dis"><a>»</a></li>';  // 下一页
    this.option = $.extend(defaults, options);

    this.el = el;   // 插入页码的容器
    this.once();
}
Page.prototype.replace = function(str, page) {
    return str.replace(/@page/g, page);
}
Page.prototype.normal = function() {
    for (var i = 1; i <= this.option.total; i++) {
        if (this.option.current == i) {
            this.html.push(this.replace(this.itemActive, i));
            continue;
        }
        this.html.push(this.replace(this.item, i));
    }
}
Page.prototype.before = function() {
    var start = this.option.total - this.option.count + 1;
    for (var i = start; i <= this.option.total; i++) {
        if (this.option.current == i) {
            this.html.push(this.replace(this.itemActive, i));
            continue;
        }
        if (i == start) {
            this.html.push(this.replace(this.item, 1));
            continue;
        }
        if (i == start + 1) {
            this.html.push(this.replace(this.itemDis, i));
            continue;
        }
        this.html.push(this.replace(this.item, i));
    }
}
Page.prototype.after = function() {
    for (var i = 1; i <= this.option.count; i++) {
        if (this.option.current == i) {
            this.html.push(this.replace(this.itemActive, i));
            continue;
        }
        if (i == this.option.count - 1) {
            this.html.push(this.replace(this.itemDis, i));
            continue;
        }
        if (i == this.option.count) {
            this.html.push(this.replace(this.item, this.option.total));
            continue;
        }
        this.html.push(this.replace(this.item, i));
    }

}
Page.prototype.justify = function() {
    this.html.push(this.replace(this.item, 1));
    this.html.push(this.itemDis);
    var start, end, diss;
    if ((this.option.count - 4) % 2) {
        diss = (this.option.count - 4 - 1) / 2;
        start = this.option.current - diss;
        end = this.option.current + diss;
    } else {
        diss = (this.option.count - 4) / 2;
        start = this.option.current - diss;
        if (start == 2) {
            start = this.option.current - diss + 1;
            end = this.option.current + diss;
        } else {
            end = this.option.current + diss - 1;
        }

    }

    for (var i = start; i <= end; i++) {
        if (this.option.current == i) {
            this.html.push(this.replace(this.itemActive, i));
            continue;
        }
        this.html.push(this.replace(this.item, i));
    }
    this.html.push(this.itemDis);
    this.html.push(this.replace(this.item, this.option.total));
}
Page.prototype.judge = function() {
    if (this.option.total <= this.option.count) {
        this.normal();
        return;
    }
    if (this.option.current > this.option.total - (this.option.count - 3)) {
        this.before();
        return;
    }
    if (this.option.current < this.option.count - 2) {
        this.after();
        return;
    }
    this.justify();
}
Page.prototype.once = function() {

    this.setPage({ current: this.option.current });
    var _this = this;
    $(this.el).on('click', 'li', function() {
        var _page = $(this).data('index');
        if (_page == 'prev') _page = _this.option.current - 1;
        if (_page == 'next') _page = _this.option.current + 1;
        if (!_page) return;
        if (_page == _this.option.current) return;

        _this.setPage({ current: _page });

        $(_this.el).trigger("change", [_page]);

    });
}

Page.prototype.setPage = function(setpage) {
    if (!setpage || !setpage.current) {
        console.error('current page undefined');
        return;
    }

    this.option.current = setpage.current;
    if (setpage.pageSize) this.option.pageSize = setpage.pageSize;
    if (setpage.total) this.option.total = Math.ceil(setpage.total / this.option.pageSize);
    this.html = [];
    this.judge();
    var prev = setpage.current == 1 ? this.itemPrev.replace(/@dis/g, 'disabled') : this.itemPrev.replace(/@dis/g, '');
    var next = setpage.current == this.option.total ? this.itemNext.replace(/@dis/g, 'disabled') : this.itemNext.replace(/@dis/g, '');

    var html = '<ul class="pagination">' + prev + this.html.join('') + next + '</ul>';
    if (!this.option.total) {
        $(this.el).addClass('hide');
        return
    } else {
        $(this.el).removeClass('hide');
    }
    if ($(this.el).find('.pagination').length) {
        $(this.el).find('.pagination').html(html);
    } else {
        $(this.el).html(html);
    }


}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

了 义

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值