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

调用代码如下:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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 类如下

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
// ==============================================================================
//
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 οnclick='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 οnclick='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 οnclick='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);
};
}

 

 

 

转载于:https://www.cnblogs.com/bndy/articles/1692191.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值