欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于jquery封装的一个js分页

程序员文章站 2022-04-15 19:49:28
代码如下: (function ($) { var pagefunc = function pagefunc() { } $.pagefunc = function (total, pagesize...

代码如下:

(function ($) {
var pagefunc = function pagefunc() { }
$.pagefunc = function (total, pagesize, curpagenum, funurl) {
if (pagesize == "" || pagesize == null || pagesize == undefined) {
pagesize = 10;
}
if (curpagenum == "" || curpagenum == null || curpagenum == undefined) {
curpagenum = 1;
}
//计算总页数
total = parseint(total); //总记录数
pagesize = parseint(pagesize); //每页显示数
curpagenum = parseint(curpagenum); //当前页
//总页数
var allpage = math.floor(total / pagesize);
if (total % pagesize != 0) {
allpage++;
}
var navhtml = "";
if (curpagenum <= 0)
curpagenum = 1;
if (allpage > 1) {
if (curpagenum != 1) {
//处理首页连接
navhtml += "<span><a href=\"javascript:" + funurl + "('1')\" >|<</a></span> ";
}
if (curpagenum > 1) {
//处理上一页的连接
navhtml += "<span><a href=\"javascript:" + funurl + "('" + (curpagenum - 1) + "')\" ><<</a></span> ";
}
else {
navhtml += "<span class=\"disabled\"><<</span> ";
}

var currint = 5;
for (var i = 0; i <= 10; i++) {
//一共最多显示10个页码,前面5个,后面5个
if ((curpagenum + i - currint) >= 1 && (curpagenum + i - currint) <= allpage)
if (currint == i) {
//当前页处理
navhtml += "<span lass=\"current\">[" + curpagenum + "]</span> ";
}
else {
//一般页处理
var n = curpagenum + i - currint;
navhtml += "<a href=\"javascript:" + funurl + "('" + (parseint(n)) + "')\">" + n + "</a> ";
}
}
if (curpagenum < allpage) {
//处理下一页的链接
navhtml += "<span><a href=\"javascript:" + funurl + "('" + (parseint(curpagenum) + 1) + "')\">>></a></span> ";
}
else {
navhtml += "<span class=\"disabled\">>></span> ";
}

if (curpagenum != allpage) {
navhtml += "<span><a href=\"javascript:" + funurl + "('" + allpage + "')\" >>|</a></span>  ";
}

}

navhtml += "<span>[" + curpagenum + "/" + allpage + "]</span> ";

return navhtml;

};

})(jquery);


下边是调用方法:

代码如下:


function querylist(curpage) {
if (curpage == "" || curpage == null || curpage == undefined) {
curpage = 1;
}
var pagesize = 10;

var countys = $("#countys").val(); //县
var enddate = $("#enddate").val(); //结束时间
var begindate = $("#begindate").val(); //开始时间
$.ajax({
url: "",
type: "post",
data: { "countys": countys, "enddate": enddate, "begindate": begindate, "curpage": curpage, "pagesize": pagesize },
datatype: "json",
error: function (xhr, status, errmsg) { window.location.href = "/commonerror/index/" + errmsg.code + "?txt=" + errmsg.msg; },
success: function (mydata) {
var str = "";
$.each(mydata.tdata, function (i, item) {

str += "<tr>"
str += "<td>" + $.formatdatetime(item.controlbegintime, false) + "</td>";
str += "<td>" + $.formatdatetime(item.controlendtime, false) + "</td>";
str += "<td>" + item.name + "</td>";
str += "<td>" + item.platenumber + "</td>";
str += "<td>" + item.controldept + "</td>";
if (item.isall == "0") {
str += "<td>全县布控</td>";
}
else {
str += "<td>按卡口点布控</td>";
}
str += "<td>" + item.isall == "0" ? "全县布控" : "按卡口点布控" + "</td>";
str += "<td>修改</td>";
str += "<td>删除</td>";
str += "<td>详细</td>";
str + "</tr>";
});

$("#srh_rslt tbody").html(str);

$(".pagination").html($.pagefunc(mydata.total, pagesize, curpage, "querylist"));
}
});
}