JS 分页函数(适合Ajax分页时使用)

调用代码如下:

代码
var p = new Pager(20, 5); // 20 为总记录数(可随意或是指定),5 为页大小
p.init('tdAA', a); //tdAA 为分页控件父容器的ID

function a() {
// AJAX获取数据
SendContent("/Admin/TopicService.asmx/GetTopicList?currentPageIndex=" + p.currentPage + "&pageSize=" + p.pageSize, "GET", "", handleTopic);
}
p.changeTotalRecordCount(totalRecord);

 

JS 类如下

 

代码
// ==============================================================================
//
Created by Bndy at 2010/3/18
//
Copyright (c) 2010 Bndy.Net, All rights reserved.
//

//
* * * * * * * * * * * * * * * *
//
* Q Q : 8 1 7 9 5 7 0 5 *
//
* M S N : bndy533@msn.com *
//
* Email : bndy533@163.com *
//
* * * * * * * * * * * * * * * *
//

//
------------------------------------------------------------------------------
//
JS 分页函数
//
适合Ajax分页时使用
//
==============================================================================

var pager;


var Pager = function(totalRecordCount, pageSize) {

pager
= this;
var handler;
this.pageSize = pageSize;
this.currentPage = 1;
this.totalRecordCount = totalRecordCount;

var ele;

this.getBeginPageNum = function() {
if (this.pageSize > this.totalPageCount) {
return 1;
}

if (this.currentPage > this.totalPageCount) {

return this.totalPageCount - this.pageSize + 1;
}
else {
return Math.floor((this.currentPage - 1) / this.pageSize) * this.pageSize + 1;
}
};

this.getEndPageNum = function() {
var x = Math.floor((this.currentPage - 1) / this.pageSize) * this.pageSize + this.pageSize;
if (this.pageSize > this.totalPageCount || this.currentPage > this.totalPageCount || x > this.totalPageCount) {
return this.totalPageCount;

}

return x;
};

this.init = function(htmlElementName, handlerMethod) {

this.totalPageCount = this.totalRecordCount % pageSize != 0 ? Math.floor(this.totalRecordCount / pageSize) + 1 : Math.floor(this.totalRecordCount / pageSize);

ele
= htmlElementName;
handler
= handlerMethod;
var beginPageNum = this.getBeginPageNum();
var endPageNum = this.getEndPageNum();
var str = "";

handler();
alert(
this.totalRecordCount);

if (this.totalRecordCount != -1) {

// 生成分页HTML字符串
if (this.currentPage > this.pageSize) {
str
= " <span onclick='pager.changePage(" + (beginPageNum - 1) + ")' style=\"cursor:pointer;\"> << </span> ";
}

for (var i = beginPageNum; i <= endPageNum; i++) {
if (i == this.currentPage) {
str
= str + " <b>" + i.toString() + "</b> ";
}
else {
str
= str + " <span onclick='pager.changePage(" + i + ")' style=\"cursor:pointer;\">" + i.toString() + "</span> ";
}

}

if (this.totalPageCount > endPageNum) {
var nextPageBeginNum = endPageNum + 1 < this.totalPageCount ? endPageNum + 1 : this.totalPageCount;
str
= str + " <span onclick='pager.changePage(" + nextPageBeginNum + ")' style=\"cursor:pointer;\"> >> </span> ";
}
}

document.getElementById(ele).innerHTML
= "";
document.getElementById(ele).innerHTML
= str;

};

this.changePage = function(currentPage) {
this.currentPage = currentPage;
this.init(ele, handler);
};

this.changeTotalRecordCount = function(totalRecordCount) {
this.totalRecordCount = totalRecordCount;
this.init(ele, handler);
};
}

 

 

 

posted @ 2010-03-23 09:18  bndy  阅读(543)  评论(0)    收藏  举报