【发布时间】:2012-05-02 01:55:48
【问题描述】:
我有这个分页脚本,除了这个小问题外,它工作得很好。 所以现在我想限制导航数量。 但我无法通过 (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"> « 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 »</span>';
pagerHtml += '<span onClick="'+pagerName+'.last();" class="pg-normal"> Last »</span>';
element.innerHTML = pagerHtml;
}
}
请点击此链接 http://jsfiddle.net/J3Qnx/16/
我正在尝试将 this.currentPage 传递给此
for (var page = 1; page <= this.currentpage + 5; page++)
但它没有像我预期的那样工作 请大家帮忙
【问题讨论】:
标签: javascript navigation pagination