【问题标题】:Pagination not working :分页不起作用:
【发布时间】:2013-08-27 08:18:10
【问题描述】:

我正在使用以下 javascript 在我的应用程序中显示分页,它对我来说工作正常,但我需要在 5 页后打破分页

这是我现有的分页

1 987654323 3 987654325 5 987654327 7 987654329 9 987654331 11 987654333 13 987654335 15 987654337 @17181920

但我需要通过以下方式显示分页

12345......15161717181920

如果我点击第 5 页,那么它应该在当前页面添加另外 5 个页面

我的 JavaScript 是这样的

<script type="text/javascript">

function Pager(tableName, itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;

    this.showRecords = function(from, to) {
        var rows = document.getElementById(tableName).rows;
        // i starts from 1 to skip table header row
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to)
                rows[i].style.display = 'none';
            else
                rows[i].style.display = '';
        }
    }
    this.showPage = function(pageNumber) {
        if (! this.inited) {
            alert("not inited");
            return;
        }
        var oldPageAnchor = document.getElementById('pg'+this.currentPage);
        oldPageAnchor.className = 'pg-normal';
        this.currentPage = pageNumber;
        var newPageAnchor = document.getElementById('pg'+this.currentPage);
        newPageAnchor.className = 'pg-selected';
        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
    }

    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }

    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    }

    this.init = function() {
        var rows = document.getElementById(tableName).rows;
        var records = (rows.length - 1);
        this.pages = Math.ceil(records / itemsPerPage);
        this.inited = true;
    }

    this.showPageNav = function(pagerName, positionId) {
        if (! this.inited) {
            alert("not inited");
            return;
        }
        var element = document.getElementById(positionId);
        var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> <img src="${ctx}/images/prev.PNG" alt=" « Prev" height="17px" width="26px" style="vertical-align: middle;"/>&nbsp; </span> ';
        for (var page = 1; page <= this.pages; page++)
        pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> ';
        pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal">&nbsp;<img src="${ctx}/images/nexts.png" alt="Next »" height="17px" width="26px" style="vertical-align: middle;"/></span>';
        element.innerHTML = pagerHtml;
    }
}


</script>

如果有任何建议将是可观的

这里是JSfiddle

【问题讨论】:

  • @TusharGupta 更新了 jsfiddle
  • 由于您的小提琴只有 CSS,我只能在这里通过您的 javascript 代码猜测:如果您的 showPageNav 函数正在打印页码,那么就是您的问题。您的 for 循环会为每一页打印一个带编号的链接。
  • @alex 我已经更新了我的小提琴,请检查一次

标签: javascript jquery html pagination


【解决方案1】:

这是一个新的 jsfiddle 做你想做的事:

http://jsfiddle.net/nye7a/2/

showPageNav 功能已更改如下:

this.showPageNav = function(pagerName, positionId) {

var pageIndex = this.currentPage,
    pageCount = this.pages,
    start = Math.max(1, pageIndex - 4),
    end = Math.min(pageCount, pageIndex + 4),
    start2 = pageCount-4,
    end2 = pageCount;

    if (start2 <= end)
        end = pageCount;

if (! this.inited) {

alert("not inited");

return;

}

var element = document.getElementById(positionId);

var pagerHtml = '';

// adds the Prev button only if needed
if (pageIndex > 1)
    pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> « Prev </span> ';

    // paging from 1 to 5   
    for (var page = start; page <= end; page++) {

pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> ';
    }

    // paging from pageCount-5 to pageCount
    if (end != pageCount) {
         pagerHtml += '<span>...</span>';

        for (var page = start2; page <= end2; page++) {

    pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> ';
        }   
    }

// adds the Next button only if needed
if (pageIndex < pageCount && pageCount > 1)    
    pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next »</span>';

element.innerHTML = pagerHtml;

}

【讨论】:

  • 感谢您的回复,但是当我点击第 5 页时并没有添加 5 页,当我点击下一个按钮时它会停止分页
  • 在 this.showPage 函数中,可以在 this.currentPage = pageNumber; : this.showPageNav('pager', 'pageNavPosition');
  • 很高兴我能帮上忙! ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
  • 2016-02-07
  • 2017-09-13
  • 2015-05-15
  • 2013-11-25
相关资源
最近更新 更多