JS Pager From DUDU
2012-05-24 08:33 迷路中的路人甲 阅读(212) 评论(0) 编辑 收藏 举报/*#region Pager*/
var Pager = {};
Pager.PageIndex = 1;
Pager.PageSize = 30;
Pager.ShowPageCount = 5;
Pager.TotalCount = 0;
Pager.UrlFormat = location.href + "?page={0}";
Pager.ClickFunctionName = "";
Pager.Build = function (node) {
$(node).html('');
var sumPage = parseInt((this.TotalCount + this.PageSize - 1) / this.PageSize);
var start = this.PageIndex - this.ShowPageCount;
var end = this.PageIndex + this.ShowPageCount;
if (sumPage > (this.ShowPageCount * 2 - 1)) {
if (start < 1) {
start = 1;
end = start + 2 * this.ShowPageCount;
}
else if (end > sumPage) {
start = sumPage - 2 * this.ShowPageCount;
end = sumPage;
}
}
else {
start = 1;
end = sumPage;
}
var fragment = document.createDocumentFragment();
if (this.PageIndex > 1) {
fragment.appendChild(this.BuildLink("< Prev", this.PageIndex - 1));
if (this.PageIndex > this.ShowPageCount) {
fragment.appendChild(this.BuildLink(1, 1));
}
if (start > 2) {
fragment.appendChild(document.createTextNode("···"));
}
}
for (i = start; i <= end; i++) {
if (i == this.PageIndex) {
var span = document.createElement("span");
$(span).prop("class", "current");
span.innerHTML = this.PageIndex;
fragment.appendChild(span);
}
else {
fragment.appendChild(this.BuildLink(i, i));
}
}
if (this.PageIndex < sumPage) {
if (end < sumPage) {
fragment.appendChild(document.createTextNode("···"));
fragment.appendChild(this.BuildLink(sumPage, sumPage, this.TotalCount));
}
fragment.appendChild(this.BuildLink("Next >", this.PageIndex + 1, this.TotalCount));
}
$(node).html(fragment);
}
Pager.BuildLink = function (pageTitle, pageIndex) {
var a = document.createElement("a");
if (this.ClickFunctionName) {
a.href = "javascript:void(0);";
var js = this.ClickFunctionName + "(" + pageIndex + ");Pager.SetCurrent(" + pageIndex + ");";
a.onclick = function() { eval(js); }
}
else {
a.href = this.UrlFormat.replace("{0}", pageIndex);
}
a.innerHTML = pageTitle;
return a;
}
Pager.SetCurrent = function (pageIndex) {
this.PageIndex = pageIndex;
if (this.PageIndex > 1) {
this.Build($("#pager_top"));
}
this.Build($("#pager_bottom"));
}
/*#endregion Pager*/