/**
*=================================================================
*name: 叶子js分页样式(showopage style with js)
*rcsfile: cls_jspage.js
*revision: 0.09
*author: yehe(叶子)
*released: 2005-05-12 23:00:15
*description: js分页样式,显示上一页下一页的翻页结果
*contact: qq:311673,msn:myehe@msn.com
*website:
*=================================================================
*/
function cls_jspage(ireccount, ipagesize, ipagenum, sname) {
this.irc = this.formatnum(ireccount, 1, 0, 0, 0); //总记录条数
this.ips = this.formatnum(ipagesize, 1, 0, 1, 0); //每页记录数目
this.ipn = this.formatnum(ipagenum, 0, 0, 0, 0); //显示的前后页数,0为显示所有,负数为这么多页面一个跳转
this.sn = sname; //实例对象名
this.stpage = "{$page}"; //页数
this.stpagecount = "{$pagecount}"; //总页数
this.streccount = "{$reccount}"; //总记录数
this.stpagesize = "{$pagesize}"; //每页记录数
this.stpagefrist = "{$pagefrist}"; //首页
this.stpageprev = "{$pageprev}"; //上页
this.stpagenext = "{$pagenext}"; //下页
this.stpagelast = "{$pagelast}"; //尾页
this.stpagetext = "{$pagetext}"; //数字跳转
this.stpagetextf = "{$pagetextf}"; //数字跳转框架
this.stpageinput = "{$pageinput}"; //输入框跳转
this.stpageselect = "{$pageselect}"; //下拉菜单跳转
this.stpagenum = "{$pagenum}"; //数字页数
this.ipc = this.getpagecount(); //得到页数
}
//输入 页数开始值,结尾值
cls_jspage.prototype.setpagese = function (spagestart, spageend) {
var sps = spagestart, spe = spageend;
this.sps = (sps.length > 0) ? sps : "page=";
this.spe = (spe.length > 0) ? spe : "";
}
//输入 网址
cls_jspage.prototype.seturl = function (surl) {
var s = surl;
this.url = (s.length > 0) ? s : "" + document.location;
}
//输入 输入框&下拉框name值
cls_jspage.prototype.setpageinput = function (spageinput) {
var spi = spageinput;
this.spi = (spi.length > 0) ? spi : "page";
}
//输入 语言 首页(disable,enale)
cls_jspage.prototype.setpagefrist = function (sdis, sen, snen) {
this.pf_d = sdis;
this.pf_e = sen;
this.pf_n = snen ? snen : sen;
}
//输入 语言 上页
cls_jspage.prototype.setpageprev = function (sdis, sen, snen) {
this.pp_d = sdis;
this.pp_e = sen;
this.pp_n = snen ? snen : sen;
}
//输入 语言 下页
cls_jspage.prototype.setpagenext = function (sdis, sen, snen) {
this.pn_d = sdis;
this.pn_e = sen;
this.pn_n = snen ? snen : sen;
}
//输入 语言 尾页
cls_jspage.prototype.setpagelast = function (sdis, sen, snen) {
this.pl_d = sdis;
this.pl_e = sen;
this.pl_n = snen ? snen : sen;
}
//输入 语言 数字跳转
cls_jspage.prototype.setpagetext = function (sdis, sen) {
this.pt_d = sdis; //"[{$pagenum}]"
this.pt_e = sen; //"第{$pagenum}页"
}
//输入 语言 数字跳转外围模板
cls_jspage.prototype.setpagetextf = function (sdis, sen) {
this.ptf_d = sdis; //" {$pagetextf} "
this.ptf_e = sen; //" {$pagetextf} "
}
//输入 语言 下拉菜单跳转
cls_jspage.prototype.setpageselect = function (sdis, sen) {
this.ps_d = sdis; //"[{$pagenum}]"
this.ps_e = sen; //"第{$pagenum}页"
}
//输入 css
cls_jspage.prototype.setpagecss = function (scsspagetext, scsspageinput, scsspageselect) {
this.cpt = scsspagetext; //数字跳转css
this.cpi = scsspageinput; //输入框跳转css
this.cps = scsspageselect; //下拉菜单跳转css
}
//输入 html模板
cls_jspage.prototype.sethtml = function (shtml) {
this.html = shtml; //html模板
}
//计算页数
cls_jspage.prototype.getpagecount = function () {
var irc = this.irc, ips = this.ips;
var i = (irc % ips == 0) ? (irc / ips) : (this.formatnum((irc / ips), 1, 0, 0, 0) + 1);
return (i);
}
//取得模板页数和当前页数
cls_jspage.prototype.geturl = function (itype) {
var s = this.url, sps = this.sps, spe = this.spe, stp = this.stpage, ipc = this.ipc;
var it = itype, i;
if (s.indexof(sps) == -1) {
s += ((s.indexof("?") == -1) ? "/?" : "&") + sps + stp+spe;
i = 1;
}
else {
sreg = "(\\s.*)" + this.formatreg(sps) + "(\\d*)" + this.formatreg(spe) + "(\\s.*|\\s*)";
var spindex = this.reg(s, sreg, "$3");
s = s.replace(sps + spindex + spe, sps + stp + spe);
i = this.formatnum(spindex, 1, 1, 0, ipc);
}
s = this.reg(s, "(&+)", "&");
s = this.reg(s, "(\\?&)", "?");
s = s.replace("//?", "/?");
return (it == 0 ? s : i);
}
//页面跳转
cls_jspage.prototype.pagejump = function () {
var spl, spv, sp;
var su = this.geturl(0), ipi = this.geturl(1);
var spi = this.spi, stp = this.stpage, ipc = this.ipc;
spl = document.getelementsbyname(spi).length;
for (var i = 0; i < spl; i++) {
spv = document.getelementsbyname(spi)[i].value;
sp = this.formatnum(spv, 1, 1, 0, ipc);
if (sp > 0) {
location.href = su.replace(stp, sp);
break;
}
}
}
cls_jspage.prototype.write = function () {
document.write(this.creathtml());
}
//输出
cls_jspage.prototype.creathtml = function () {
var su = this.geturl(0), ipi = this.geturl(1);
var sn = this.sn, spi = this.spi;
var ipc = this.ipc, ipn = this.ipn; ;
var irc = this.irc, ips = this.ips;
var pf_d = this.pf_d, pf_e = this.pf_e, pf_n = this.pf_n;
var pp_d = this.pp_d, pp_e = this.pp_e, pp_n = this.pp_n;
var pn_d = this.pn_d, pn_e = this.pn_e, pn_n = this.pn_n;
var pl_d = this.pl_d, pl_e = this.pl_e, pl_n = this.pl_n;
var pt_d = this.pt_d, pt_e = this.pt_e;
var ptf_d = this.ptf_d, ptf_e = this.ptf_e;
var ps_d = this.ps_d, ps_e = this.ps_e;
var cpt = this.cpt, cpi = this.cpi;
var cps = this.cps, ipn = this.ipn;
var s = this.html;
stpage = this.stpage;
stpagecount = this.stpagecount;
streccount = this.streccount;
stpagesize = this.stpagesize;
stpfrist = this.stpagefrist;
stpprev = this.stpageprev;
stpnext = this.stpagenext;
stplast = this.stpagelast;
stptext = this.stpagetext;
stptextf = this.stpagetextf;
stpinput = this.stpageinput;
stpselect = this.stpageselect;
stpagenum = this.stpagenum;
var prevp = this.formatnum((ipi - 1), 1, 1, 1, ipc), nextp = this.formatnum((ipi + 1), 1, 1, 1, ipc);
var fu, pu, nu, lu;
var s1 = "", s3 = "";
var s4 = "", s5 = "";
if (ipi <= 1 && ipc <= 1) {
fu = s4 + pf_n + s5;
pu = s4 + pp_n + s5;
nu = s4 + pn_n + s5;
lu = s4 + pl_n + s5;
}
else if (ipi == 1 && ipc > 1) {
fu = s4 + pf_n + s5;
pu = s4 + pp_n + s5;
nu = s1 + su.replace(stpage, nextp) + s2 + pn_e + s3;
lu = s1 + su.replace(stpage, ipc) + s2 + pl_e + s3;
}
else if (ipi == ipc) {
fu = s1 + su.replace(stpage, 1) + s2 + pf_e + s3;
pu = s1 + su.replace(stpage, prevp) + s2 + pp_e + s3;
nu = s4 + pn_n + s5;
lu = s4 + pl_n + s5;
}
else {
fu = s1 + su.replace(stpage, 1) + s2 + pf_e + s3;
pu = s1 + su.replace(stpage, prevp) + s2 + pp_e + s3;
nu = s1 + su.replace(stpage, nextp) + s2 + pn_e + s3;
lu = s1 + su.replace(stpage, ipc) + s2 + pl_e + s3;
}
var pagestart, pageend;
if (ipn < 0) {
ipn = math.abs(ipn);
pagestart = (ipi % ipn == 0) ? (ipi / ipn) : (this.formatnum((ipi / ipn), 1, 0, 0, 0));
pagestart = (pagestart * ipn == ipi) ? ((pagestart - 1) * ipn + 1) : (pagestart * ipn + 1);
pageend = this.formatnum(pagestart + ipn, 0, 1, 0, ipc)
}
else if (ipn == 0) {
pagestart = 1;
pageend = ipc;
}
else {
pagestart = this.formatnum((ipi - ipn), 1, 0, 1, 0);
pageend = this.formatnum((pagestart + ipn * 2), 0, 1, 0, ipc);
pagestart = (pageend == ipc) ? this.formatnum((pageend - ipn * 2), 1, 0, 1, 0) : pagestart;
}
var pselect = "", ptext = "", pinput = "", p;
if (ipc >= 1) {
pselect = "";
}
s = s.replace(stpage, ipi);
s = s.replace(stpagecount, ipc);
s = s.replace(streccount, irc);
s = s.replace(stpagesize, ips);
s = s.replace(stpfrist, fu);
s = s.replace(stpprev, pu);
s = s.replace(stpnext, nu);
s = s.replace(stplast, lu);
s = s.replace(stptext, ptext);
s = s.replace(stpinput, pinput);
s = s.replace(stpselect, pselect);
return s;
}
//输入:欲格式化字符,是否有最小值(0表示没有,1表示有),是否有最大值,最小值(默认值),最大值
cls_jspage.prototype.formatnum = function (snum, bmin, bmax, iminnum, imaxnum) {
var i, in, sn = "" + snum, imin = iminnum, imax = imaxnum;
if (sn.length > 0) {
in = parseint(sn, 10);
i = (isnan(in)) ? imin : in;
i = (i < imin && bmin == 1) ? imin : i;
i = (i > imax && bmax == 1) ? imax : i;
}
else {
i = imin;
}
return (i);
}
//输入:欲正则字符,正则表达式,替换后字符
cls_jspage.prototype.reg = function (sstr, sreg, sre) {
var s = "", ss = sstr, sr = sreg, sre = sre;
if ((ss.length > 0) && (sr.length > 0)) {
eval("re=/" + sr + "/gim;");
s = ss.replace(re, sre);
}
return (s);
}
//格式化正则中的特殊字符
cls_jspage.prototype.formatreg = function (sreg) {
var s = "", sr = sreg;
var sf = new array("/", ".", "+", "[", "]", "{", "}", "$", "^", "?", "*");
if (sr.length > 0) {
for (var i = 0; i <= sf.length; i++) {
sr = sr.replace(sf[i], "\\" + sf[i]);
}
s = "(" + sr + ")";
}
return (s);
}