JS 分页实现
var pagesize = 5;
var pageindex = 1;
var pagetype;
var parms;
var nextSize = 1;
var target;
function AjaxGetData(_pagesize, _pageindex, _type, _parms, _target) {
pagesize = _pagesize;
pageindex = _pageindex;
pagetype = _type;
parms = _parms;
target = _target;
$.ajax({
url: "/pro/casxIndex.ashx?PageType=" + pagetype + "&time=" + Date.parse(new Date()) + "",
type: "post",
data: "PageSize=" + pagesize + "&PageIndex=" + pageindex + parms,
dataType: "json",
success: function (data) {
var targetHtml = "";
var targetEle = "";
if (target) {
targetEle = $(target)
};
if (data.result == 1) {
var returnJson = $.parseJSON(data.returnval);
targetHtml += MatchCss(pagetype, returnJson);
}
if (targetHtml != "undefined") {
if (data.result == "2") {
targetEle.html(data.returnval);
}
else {
targetEle.html(targetHtml);
}
}
}
})
}
function MatchCss(pagetype, returnJson) {
switch (pagetype.toLocaleLowerCase()) {
case "caseindex":
return caseIndexList("", returnJson);
break
}
}
function caseIndexList(url, returnJson) {
var targetHtml = "";
targetHtml += " <ul class=\"wms-caseList\"> ";
for (var i = 0; i < returnJson.length; i++) {
targetHtml += "<li>";
targetHtml += "<div><a href=" + returnJson[i].rootlist + " title=\"\"><img src=" + returnJson[i].ImageURL + " alt=" + returnJson[i].Title + " width=\"280\" height=\"156\"></a></div>";
targetHtml += "<dl>";
targetHtml += "<dt>";
targetHtml += "<a href=" + returnJson[i].rootlist + ">" + returnJson[i].Title + "</a>";
targetHtml += "<span style=\"font-size:xx-small;float:right;\">" + returnJson[i].AddTime + "</span>"
targetHtml += "</dt>";
targetHtml += "<dd>" + returnJson[i].Remark + "</dd>";
targetHtml += "</dl>";
targetHtml += "</li>"
}
targetHtml += "</ul>";
targetHtml += caseIndexPager(returnJson);
return targetHtml
}
function caseIndexPager(returnJson) {
var targetHtml = "";
if (parseInt(returnJson[0].count) > pagesize) {
targetHtml += "<div class=\"page\" >";
targetHtml += "<span style='display:none;'>共有记录" + returnJson[0].count + ";共<span id='count'>" + (returnJson[0].count % pagesize == 0 ? parseInt(returnJson[0].count / pagesize) : parseInt(returnJson[0].count / pagesize + 1)) + "</span>页</span>";
targetHtml += "<span class=\"sp11\"><a href='javascript:void' onclick='GoToFirstPage()' id='aFirstPage' title='首页' >首页</a></span>\n";
targetHtml += "<span class=\"sp12\"><a href='javascript:void' onclick='GoToPrePage()' id='aPrePage' title='上一页'>上一页</a></span>\n";
if (pagesize < parseInt(returnJson[0].count)) {
targetHtml += "<span class='num_2'>\n";
for (var i = 1; i < 6; i++) {
if (((nextSize) * 5 - 5 + i) <= (parseInt(returnJson[0].count) % pagesize == 0 ? parseInt(returnJson[0].count / pagesize) : parseInt(returnJson[0].count / pagesize + 1))) {
if ((nextSize) * 5 - 5 + i == pageindex) {
targetHtml += "<a style='color:red;' title='正在查询" + (((nextSize) * 5 - 5 + i - 1) * pagesize + 1) + " to " + ((((nextSize) * 5 - 5 + i) * pagesize) > returnJson[0].count ? returnJson[0].count : (((nextSize) * 5 - 5 + i) * pagesize)) + " / " + returnJson[0].count + "' href='javascript:void' onclick='GoToChoosePage(this)' id='aChoosePage" + i + "' title=''>" + ((nextSize) * 5 - 5 + i) + " </a>"
} else {
targetHtml += "<a title='正在查询" + (((nextSize) * 5 - 5 + i - 1) * pagesize + 1) + " to " + ((((nextSize) * 5 - 5 + i) * pagesize) > returnJson[0].count ? returnJson[0].count : (((nextSize) * 5 - 5 + i) * pagesize)) + " / " + returnJson[0].count + "' href='javascript:void' onclick='GoToChoosePage(this)' id='aChoosePage" + i + "' title=''>" + ((nextSize) * 5 - 5 + i) + " </a>"
}
} else {
break
}
}
targetHtml += "</span>\n"
} else {
targetHtml += "<a style='color:red;' title='正在查询" + ((pageindex - 1) * pagesize + 1) + " to " + returnJson[0].count + " / " + returnJson[0].count + "' href='javascript:void' onclick='GoToChoosePage(this)' id='aChoosePage' title=''>" + pageindex + " </a>"
}
targetHtml += "<span class=\"sp13\"><a href='javascript:void' onclick='GoToNextPage()' id='aNextPage'title='下一页'>下一页</a></span>\n";
targetHtml += "<span class=\"sp14\"><a href='javascript:void' onclick='GoToEndPage()' id='aEndPage' title='末页' >末页</a></span>\n";
targetHtml += "</div>"
}
return targetHtml
}
function GoToFirstPage() {
pageindex = 1;
nextSize = parseInt(pageindex / 5 + 1);
AjaxGetData(pagesize, pageindex, pagetype, parms, target)
}
function GoToPrePage() {
if (pageindex > 1) {
pageindex -= 1;
nextSize = parseInt((pageindex - 1) / 5 + 1);
pageindex = pageindex > 0 ? pageindex : 1;
AjaxGetData(pagesize, pageindex, pagetype, parms, target)
}
}
function GoToNextPage() {
if (pageindex < parseInt($("#count").text())) {
pageindex += 1;
nextSize = parseInt((pageindex - 1) / 5 + 1)
}
AjaxGetData(pagesize, pageindex, pagetype, parms, target)
}
function GoToEndPage() {
pageindex = parseInt($("#count").text());
nextSize = parseInt((pageindex - 1) / 5 + 1);
AjaxGetData(pagesize, pageindex, pagetype, parms, target)
}
function GoToChoosePage(mm) {
pageindex = parseInt(mm.innerHTML);
nextSize = parseInt((pageindex - 1) / 5 + 1);
AjaxGetData(pagesize, pageindex, pagetype, parms, target)
}
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
}
if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
return format
}