显示的Javascript分页更改页码

时间:2018-09-27 22:04:41

标签: javascript pagination

我有一个表的分页脚本,该脚本根据表中的元素数量生成页码,问题是它显示了所有页面,例如,如果有50个页面,则会用50个按钮填充页面但我只想显示前3-4个按钮,然后...再显示最后一页按钮。 我试图对其进行修改,但是我不确定自己做错了什么,因为它实际上没有做任何事情

$.fn.pageMe = function(opts) {
    var $this = this, defaults = {
        perPage : 7,
        showPrevNext : false,
        hidePageNumbers : false
    }, settings = $.extend(defaults, opts);

    var listElement = $this;
    var perPage = settings.perPage;
    var children = listElement.children();
    var pager = $('.pager');

    if (typeof settings.childSelector != "undefined") {
        children = listElement.find(settings.childSelector);
    }

    if (typeof settings.pagerSelector != "undefined") {
        pager = $(settings.pagerSelector);
    }

    var numItems = children.length;
    var numPages = Math.ceil(numItems / perPage);

    pager.data("curr", 0);

    if (settings.showPrevNext) {
        $('<li class="page-item"><a href="#" class="prev_link">«</a></li>')
                .appendTo(pager);
    }

    var curr = 0;
    while (numPages > curr && (settings.hidePageNumbers === false)) {
        $(
                '<li class="page-item"><a href="#" class="page-link">'
                        + (curr + 1) + '</a></li>').appendTo(pager);
        curr++;
    }

    if (settings.showPrevNext) {
        $('<li class="page-item"><a href="#" class="next_link">»</a></li>')
                .appendTo(pager);
    }

    pager.find('.page-link:first').addClass('active');
    pager.find('.prev_link').hide();
    if (numPages <= 1) {
        pager.find('.next_link').hide();
    }
    pager.children().eq(1).addClass("active");

    children.hide();
    children.slice(0, perPage).show();

    pager.find('li .page-link').click(function() {
        var clickedPage = $(this).html().valueOf() - 1;
        goTo(clickedPage, perPage);
        return false;
    });
    pager.find('li .prev_link').click(function() {
        previous();
        return false;
    });
    pager.find('li .next_link').click(function() {
        next();
        return false;
    });

    function previous() {
        var goToPage = parseInt(pager.data("curr")) - 1;
        goTo(goToPage);
    }

    function next() {
        goToPage = parseInt(pager.data("curr")) + 1;
        goTo(goToPage);
    }

    function goTo(page) {
        var startAt = page * perPage, endOn = startAt + perPage;

        children.css('display', 'none').slice(startAt, endOn).show();

        if (page >= 1) {
            pager.find('.prev_link').show();
        } else {
            pager.find('.prev_link').hide();
        }

        if (page < (numPages - 1)) {
            pager.find('.next_link').show();
        } else {
            pager.find('.next_link').hide();
        }

        pager.data("curr", page);
        pager.children().removeClass("active");
        pager.children().eq(page + 1).addClass("active");

    }
};

0 个答案:

没有答案