首先,要明确我是javascript和jquery的新手。
现在,我正在使用一个javascript文件在div内部分页。该脚本工作正常,但如果有大量数据,那么我需要使用“next”和“previous”标签限制分页链接。我找不到办法做到这一点。目前,分页脚本显示所有数字,如1 2 3 4 5 6 7 8 9
等,具体取决于提供的数据。但是,我希望将它们显示为1 2 3 4 5 next>>
或<<prev 2 3 4 5 6 next>>
这是我的pagination.js
var Imtech = {};
Imtech.Pager = function() {
this.paragraphsPerPage = 3;
this.currentPage = 1;
this.pagingControlsContainer = "#pagingControls";
this.pagingContainerPath = "#content";
this.numPages = function() {
var numPages = 0;
if (this.paragraphs != null && this.paragraphsPerPage != null) {
numPages = Math.ceil(this.paragraphs.length / this.paragraphsPerPage);
}
return numPages;
};
this.showPage = function(page) {
this.currentPage = page;
var html = "";
for (var i = (page-1)*this.paragraphsPerPage; i < ((page-1)*this.paragraphsPerPage) + this.paragraphsPerPage; i++) {
if (i < this.paragraphs.length) {
var elem = this.paragraphs.get(i);
html += "<" + elem.tagName + ">" + elem.innerHTML + "</" + elem.tagName + ">";
}
}
$(this.pagingContainerPath).html(html);
renderControls(this.pagingControlsContainer, this.currentPage, this.numPages());
}
var renderControls = function(container, currentPage, numPages) {
var pagingControls = "<b>Pages</b>: <ul>";
for (var i = 1; i <= numPages; i++) {
if (i != currentPage) {
pagingControls += "<li><a href='#' onclick='pager.showPage(" + i + "); return false;'>" + i + "</a></li>";
} else {
pagingControls += "<li>" + i + "</li>";
}
}
pagingControls += "</ul>";
$(container).html(pagingControls);
}
}
为了获得理想的外观,如何编辑此脚本?如果有人可以帮助我,请执行此操作。
答案 0 :(得分:2)
您可以像这样更改renderControls
:
var renderControls = function(container, currentPage, numPages, pagesCutoff) {
var pagingControls = "<b>Pages</b>: <ul>";
var prevPosition = currentPage - pagesCutoff;
var nextPosition = currentPage + pagesCutoff;
for (var i = 1; i <= numPages; i++) {
if (i != currentPage) {
if(i >= prevPosition && i <= nextPosition) {
var linkToPage = i == prevPosition ? currentPage - 1 : i == nextPosition ? currentPage + 1 : i;
var linkText = i == prevPosition ? "<< prev" : i == nextPosition ? "next >>" : i;
pagingControls += "<li><a href='#' onclick='pager.showPage(" + linkToPage + "); return false;'>" + linkText + "</a></li>";
}
} else {
pagingControls += "<li>" + i + "</li>";
}
}
pagingControls += "</ul>";
$(container).html(pagingControls);
}
并将其添加到Imtech.Pager
:this.pageLinksEachSide = 3;
并将renderControls
调用更改为:renderControls(this.pagingControlsContainer, this.currentPage, this.numPages(), this.pageLinksEachSide);
这假设您想要&#34; next&#34;链接导航至currentPage + 1
和&#34; prev&#34;链接以导航至currentPage - 1
。