這篇文章給大家介紹AJAX中怎么實現(xiàn)一個分頁類,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)專注于網(wǎng)站建設,為客戶提供成都網(wǎng)站建設、成都做網(wǎng)站、網(wǎng)頁設計開發(fā)服務,多年建網(wǎng)站服務經(jīng)驗,各類網(wǎng)站都可以開發(fā),成都品牌網(wǎng)站建設,公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設計,建網(wǎng)站費用,建網(wǎng)站多少錢,價格優(yōu)惠,收費合理。
代碼如下:
/**
* pagination.js
*
通用的基于AJAX的分頁類
* @author jeanwendy
* @version 1.0
*/
var paginationIndex = 0;
var pagination = function(trTemplatId) {
$().ajaxStart(function() {
$.blockUI({
message : '
'
});
}).ajaxStop($.unblockUI);
paginationIndex = paginationIndex + 1;
this.id = paginationIndex;
this.trTemplatId = trTemplatId;
this.pageNo = 1;
this.pageSize = 10;
this.beforeQuery = null;
this.afterQuery = null;
this.url = null;
this.params = null;
this.templat = null;
this.childrenCount = null;
this.setPageNo = function(pageNo) {
if (pageNo != null)
this.pageNo = pageNo;
}
this.setPageSize = function(pageSize) {
if (pageSize != null)
this.pageSize = pageSize;
}
this.setBeforeQuery = function(fn){
this.beforeQuery = fn;
}
this.setAfterQuery = function(fn){
this.afterQuery = fn;
}
this.load = function(url, params) {
//初始化(只在第一次查詢時執(zhí)行)
if(this.templat == null && this.childrenCount == null){
var templatObj = $('#'+this.trTemplatId);
templatObj.parent().attr('id','tbody_id'+this.id);
templatObj.removeAttr('id');
templatObj.wrap("
");
this.templat = $('#divTemplat').html();
$('#divTemplat').remove();
this.childrenCount = $('#tbody_id'+this.id).children().size();
}
//開始查詢
this.url = url;
if(params == null) params = {};
$.extend(params,{pageNo:this.pageNo,pageSize:this.pageSize});
this.params = params;
var thisObj = this;
var options = {
url : url,
data : params,
async : false, //采用同步方式請求
type : 'POST',
dataType : 'json',
error : function(xmlhttp, errInfo, e) { //請求出錯處理:如:404等
if (xmlhttp.status == 200) alert('您已經(jīng)很長時間沒有訪問網(wǎng)站,請退出后重新登陸!');
else alert('請求后臺服務時發(fā)生錯誤:' + xmlhttp.status);
},
success : function(data){
//刪除上一次的數(shù)據(jù)
$('#tbody_id'+thisObj.id).children().filter(':gt('+(thisObj.childrenCount-1)+')').remove();
thisObj.pageList(data.data);
thisObj.pageBar(data.total);
if($.isFunction(thisObj.afterQuery)) thisObj.afterQuery();
}
};
if($.isFunction(this.beforeQuery)) this.beforeQuery();
$.ajax(options); //發(fā)送請求
}
this.pageList = function(data){
var filedArr = this.templat.match(/\{[A-Za-z0-9_]+\}/ig);
for(var i = 0;i < data.length;i++){
var thisTemplat = this.templat;
for(var j = 0;j < filedArr.length;j++){
var key = filedArr[j].substring(1,filedArr[j].length-1);
if(key == 'NO_'){ //序號標識
var value = (this.pageNo-1)*this.pageSize + i + 1;
thisTemplat = thisTemplat.replace(new RegExp('\{'+key+'\}','gm'),value);
}else{
var value = data[i][key];
if(typeof(value) != "undefined" && value == null) value = '';
thisTemplat = thisTemplat.replace(new RegExp('\{'+key+'\}','gm'),value);
}
}
$(thisTemplat).appendTo($('#tbody_id'+this.id));
}
}
this.pageBar = function(total){
var templatObj = $(this.templat);
var delChildren = templatObj.children(':gt(0)');
delChildren.remove();
templatObj.children().attr('colspan',$(this.templat).children().size());
templatObj.children().attr('align','right');
var pageCount;
if(total % this.pageSize == 0) pageCount = total/this.pageSize;
else pageCount = parseInt(total/this.pageSize) + 1;
if(pageCount == 0) pageCount = 1;
var toolbar = "第"+this.pageNo+"/"+pageCount+"頁("+total+"條記錄)";
if(this.pageNo == 1) toolbar = toolbar + " 首頁 上頁";
else toolbar = toolbar + "
首頁 上頁";
if(this.pageNo == pageCount) toolbar = toolbar + " 下頁 末頁";
else toolbar = toolbar + "
下頁 末頁";
toolbar = toolbar + " 每頁
條";
toolbar = toolbar + "
";
toolbar = toolbar + "
";
templatObj.children().html(toolbar);
$(templatObj.wrap("
").parent().html()).appendTo($('#tbody_id'+this.id));
var thisObj = this;
$('#firstPage'+thisObj.id).click(function(){
thisObj.pageNo = 1;
thisObj.load(thisObj.url,thisObj.params);
return false;
});
$('#prePage'+thisObj.id).click(function(){
thisObj.pageNo = parseInt(thisObj.pageNo) - 1;
thisObj.load(thisObj.url,thisObj.params);
return false;
});
$('#nextPage'+thisObj.id).click(function(){
thisObj.pageNo = parseInt(thisObj.pageNo) + 1;
thisObj.load(thisObj.url,thisObj.params);
return false;
});
$('#lastPage'+thisObj.id).click(function(){
thisObj.pageNo = pageCount;
thisObj.load(thisObj.url,thisObj.params);
return false;
});
$('#pageSize'+thisObj.id).keydown(function(e){
if(e.keyCode==13) {
var v = $('#pageSize'+thisObj.id).val();
if(!isIntGreatZero(v) || v == '0'){
alert('您輸入顯示條數(shù)不合法,請重新輸入!');
$("#pageSize"+thisObj.id).focus();
return;
}
if(v > 200){
alert('您輸入顯示條數(shù)過大了,請重新輸入!');
$("#pageSize"+thisObj.id).focus();
return;
}
thisObj.pageNo = 1;
thisObj.pageSize = v;
thisObj.load(thisObj.url,thisObj.params);
}
});
$('#pageNo'+thisObj.id).keydown(function(e){
if(e.keyCode==13) {
$('#goPage'+thisObj.id).triggerHandler('click');
}
});
$('#goPage'+thisObj.id).click(function(){
var v = $('#pageNo'+thisObj.id).val();
if(!isIntGreatZero(v) || v == '0'){
alert('您輸入頁數(shù)不合法,請重新輸入!');
$("#pageNo"+thisObj.id).focus();
return;
}
if(v > pageCount){
alert('您輸入頁數(shù)大于總頁數(shù),請重新輸入!');
$("#pageNo"+thisObj.id).focus();
return;
}
thisObj.pageNo = v;
thisObj.load(thisObj.url,thisObj.params);
});
}
}
//true if the string is empty
var isEmpty = function(text) {
var isEmpty = true;
for (var i = 0; i < text.length; i++) {
if (text.charAt(i) != ' ') {
isEmpty = false;
break;
}
}
return isEmpty;
}
//true if the string is int and great than zero or equals zero
var isIntGreatZero = function(str) {
if (isEmpty(str))
return false;
var temp1 = true;
var temp2 = '0123456789';
for (var i = 0; i < str.length; i++) {
var c = str.charAt(i);
if (temp2.indexOf(c) == -1) {
temp1 = false;
break;
} else {
if (c == '0' && i == 0 && str.length > 1) {
temp1 = false;
break;
}
}
}
return temp1;
}
關于AJAX中怎么實現(xiàn)一個分頁類就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
標題名稱:AJAX中怎么實現(xiàn)一個分頁類
文章地址:
http://weahome.cn/article/jsgoci.html