限制分页链接号码javascript

时间:2012-03-02 03:59:25

标签: javascript navigation pagination

我有这个分页脚本,除了这个小问题外,它完美无缺。 所以现在我想限制导航号码。 但我无法通过(this.currentPage)

window.thisPager= new Pager('comments', 10);
thisPager.init();
thisPager.showPageNav('thisPager', 'pageNavPosition');
thisPager.showPage(1);



function Pager(class_name, itemsPerPage) {
    this.class_name = class_name;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = pageNumber;
    this.pages = 0;
    this.inited = true;

    this.showRecords = function(from, to) {        
        var rows = $('.' + class_name);
        for (var i = 0; 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;
        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.last = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.pages);
        }
    } 

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

    this.init = function() {
        var rows = $('.' + class_name);
        var records = (rows.length); 
        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+'.first();" class="pg-normal"> « First</span>';
        pagerHtml += '<span onClick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span> | ';

        for (var page = 1; page <= this.currentpages; page++) 

            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onClick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | ';
        pagerHtml += '<span onClick="'+pagerName+'.next();" class="pg-normal"> Next &#187;</span>';
pagerHtml += '<span onClick="'+pagerName+'.last();" class="pg-normal"> Last &#187;</span>';

        element.innerHTML = pagerHtml;
    }
}

请点击此链接 http://jsfiddle.net/J3Qnx/16/

我正在尝试将this.currentPage传递给此

    for (var page = 1; page <= this.currentpage + 5; page++) 

但它没有像我预期的那样工作 请帮助人

2 个答案:

答案 0 :(得分:0)

看起来这是使用错误案例的问题。看起来您在整个脚本中使用了 currentPage ,但是您尝试在for循环中调用 currentpage 。尝试修理案例,看看是否有效。

答案 1 :(得分:0)

var class_name = "comments";
var rows = $('.' + class_name);
var itemsPerPage = 10;
var currentPage = 1;
var inited = false;
var pagerName = "thisPager";
var positionId = "pageNavigation";

var records = $('.' + class_name).length;
var pages = Math.ceil(records / itemsPerPage);


showRecords(0, 9);
navigator(currentPage, positionId, pages);


function showRecords(from, to){
for (var i = 0; i < rows.length; i++) {
    if (i < from || i > to)  
        rows[i].style.display = 'none';
    else
        rows[i].style.display = '';
}}


function showPage(pageNumber) {
currentPage = pageNumber;
var from = (currentPage - 1) * itemsPerPage;
var to = from + itemsPerPage - 1;
var positionId = "pageNavigation";
var records = $('.' + class_name).length;
var pages = Math.ceil(records / itemsPerPage);
showRecords(from, to, rows);
navigator(currentPage, positionId, pages);
var oldPageAnchor = document.getElementById('pg'+pageNumber);
oldPageAnchor.className = "pg-selected";

}

function navigator(currentPage, positionId, pages){    



var element = document.getElementById(positionId);

var pagerHtml = '';
for (var page = currentPage - 4; page <= currentPage + 4; page++)
if (page >= 1)
if (page <= pages)


    pagerHtml += '<span id="pg' + page + '" class="pg-normal" onClick="showPage(' + page + ');">' + page + '</span> | ';
element.innerHTML = pagerHtml; 


}
​