Ajax-based page-class implementation code _AJAX related

Source: Internet
Author: User
Tags error handling prev
Copy Code code as follows:

/**
* <p>pagination.js
* <p> Common AJAX-based paging classes
* @author Jeanwendy
* @version 1.0
*/
var paginationindex = 0;
var pagination = function (Trtemplatid) {
$ (). Ajaxstart (function () {
$.blockui ({
Message: ' <table><tr><td style= "Vertical-align:bottom" ><font size=2pt> load data, please ... </ Font></td></tr></table> '
});
}). 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) {
Initialize (performed only on first query)
if (This.templat = = NULL && This.childrencount = null) {
var templatobj = $ (' # ' +this.trtemplatid);
Templatobj.parent (). attr (' id ', ' tbody_id ' +this.id);
TEMPLATOBJ.REMOVEATTR (' id ');
Templatobj.wrap ("<div id= ' Divtemplat ' ></div>");
This.templat = $ (' #divTemplat '). html ();
$ (' #divTemplat '). Remove ();
This.childrencount = $ (' #tbody_id ' +this.id). Children (). Size ();
}
Start query
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,//using synchronous mode request
Type: ' POST ',
DataType: ' JSON ',
Error:function (XMLHTTP, Errinfo, E) {//Request error handling: such as: 404
if (xmlhttp.status = =) Alert (' You have not visited the site for a long time, please sign out and login again! ');
else alert (' error when requesting background service: ' + xmlhttp.status ');
},
Success:function (data) {
Delete last data
$ (' #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); Send Request
}

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_ ') {//ordinal ID
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+" page ("+total+" record);
if (This.pageno = = 1) toolbar = toolbar + "First Prev";
else toolbar = toolbar + "<a href= ' id= ' firstpage" +this.id+ "' > Home </a> <a href= ' id= ' prepage ' +this.id+ ' gt; prev </a> ";
if (This.pageno = = pagecount) toolbar = toolbar + "next Page last";
else toolbar = toolbar + <a href= ' id= ' nextPage ' +this.id+ ' ' > next page </a> <a href= ' ' id= ' lastpage ' +this.id+ ' ' & gt; last </a> ";
toolbar = toolbar + "<input per page style= ' text-align:center;width:25px;height:20px;border:1 solid black ' type= ' text ' id= ' PageSize "+this.id+" "value=" +this.pagesize+ "/> Strip";
toolbar = toolbar + "<input style=" text-align:center;width:25px;height:20px;border:1 solid black ' type= ' text ' id= ' PageNo "+this.id+" "value=" +this.pageno+ "/>";
toolbar = toolbar + "<input style= ' height:20px;border:1 solid black ' id= ' gopage ' +this.id+" ' type= ' button ' value= ' Go ' & gt; ";
Templatobj.children (). HTML (toolbar);
$ (Templatobj.wrap ("<div></div>"). 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 (' You enter the number of display bar is not valid, please re-enter! ');
$ ("#pageSize" +thisobj.id). focus ();
Return
}
if (V > 200) {
Alert (' You enter the number of display bar too large, please re-enter! ');
$ ("#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 (' You enter the number of pages is not valid, please re-enter! ');
$ ("#pageNo" +thisobj.id). focus ();
Return
}
if (V > PageCount) {
Alert (' You enter more than the total number of pages, please re-enter! ');
$ ("#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 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;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.