日期:2014-05-16  浏览次数:20374 次

JavaScript分页处理
//分页对象,按下面方式调用
//new PageManage().process(current_page, total_record_num, every_page_nums,func_name, page_display_num);
//current_page为当前页号,total_record_num总记录数,every_page_nums每页数据记录数,func_name页面的提交表单函数名,//page_display_num页面显示的页码数

var PageManage = function() {
};

PageManage.prototype = {
process : function(current_page, total_record_num, every_page_nums,
func_name, page_display_num) {

this.current_page = current_page;
this.total_record_num = total_record_num;
this.every_page_nums = every_page_nums;
this.func_name = func_name;
this.page_display_num = page_display_num || 10;

this.calc_page_var();
var pos = this.get_be_page();

if (this.page_num <= 1) {return;}
var tags = this.prepare(pos);
this.output(tags);
},

calc_page_var : function() {
var current_page = this.current_page;
var page_num = this.page_num;
var total_record_num = this.total_record_num;
var every_page_nums = this.every_page_nums;
var page_display_num = this.page_display_num;

if (current_page <= 0) {
this.current_page = 1;
}

this.page_num = 1;

if (total_record_num > 0) {
this.page_num = Math.ceil(total_record_num / every_page_nums);
}
if (current_page > page_num) {
this.current_page = page_num;
}
if (page_num < page_display_num) {
this.page_display_num = page_num;
}
},

get_be_page : function() {
var current_page = this.current_page;
var page_num = this.page_num;
var page_display_num = this.page_display_num;

var bgnp = 0;
var endp = 0;
var half_endp = Math.floor(page_display_num / 2);

if (current_page <= half_endp + page_display_num % 2) {
bgnp = 1;
if (page_num < page_display_num) {
endp = page_num;
}
else {
endp = page_display_num;
}
} else if (current_page > page_num - half_endp - page_display_num % 2) {
endp = page_num;
bgnp = page_num - page_display_num + 1;
if (bgnp < 1) {
bgnp = 1;
}
} else {
bgnp = current_page - half_endp;
endp = current_page + half_endp - 1 + page_display_num % 2;
}
var pos = {"bgn" :bgnp, "end" :endp};
return pos;
},

prepare : function(pos) {

var bgnp = pos["bgn"];
var endp = pos["end"];

var func_name = this.func_name;
var current_page = this.current_page;
var page_num = this.page_num;
var total_record_num = this.total_record_num;

var tags = {
"first_page" :"",
"prev_page" :"",
"page_list" :"",
"next_page" :"",
"last_page" :"",

"total_page" :"",
"total_record" :"",
"curr_page" :"",
"jump_page" :""
};

if (current_page > 1) {
tags["first_page"] = "".concat("<a href='javascript:", func_name,
"(1)'>首页</a>|");
tags["prev_page"] = "".concat("<a href='javascript:", func_name,
"(", (current_page - 1), ")'>上一页</a>|");
} else {
tags["first_page"] = "".concat("<span style='color:gray'>首页</span>|");
tags["prev_page"] = "".concat("<span style='color:gray'>上一页</span>|");
}

for ( var i = bgnp; i <= endp; i+=1) {
if (i != current_page) {
tags["page_list"] += "".concat("<a href='javascript:",
func_name, "(", i, ")'>", i, "</a> ");
}
else {
tags["page_list"] += "".concat("<b>", i, "</b> ");
}
}
if (current_page < page_num) {
tags["next_page"] = "".concat("<a hre