Ajax-based paging implementation code

Source: Internet
Author: User

CopyCode The Code is as follows :/**
* <P> pagination. js
* <P> General Ajax-based paging class
* @ 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 wait... </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 ){
// Initialization (only executed during the 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 = require ('{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, // synchronous request
Type: 'post ',
Datatype: 'json ',
Error: function (XMLHTTP, errinfo, e) {// request error handling: for example, 404
If (XMLHTTP. Status = 200) Alert ('You have not visited the website for a long time. Please exit and log on again! ');
Else alert ('error occurred when requesting background services: '+ XMLHTTP. status );
},
Success: function (data ){
// Delete the 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 a 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 _ ') {// Serial Number ID
VaR value = (this. pageNo-1) * This. pagesize + I + 1;
Thistemplat = thistemplat. Replace (New Regexp ('\ {' + key + '\}', 'G'), value );
} Else {
VaR value = data [I] [Key];
If (typeof (value )! = "Undefined" & value = NULL) value = '';
Thistemplat = thistemplat. Replace (New Regexp ('\ {' + key + '\}', 'G'), 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 +" Records )";
If (this. pageno = 1) toolbar = toolbar + "Homepage ";
Else toolbar = toolbar + "<a href ='' id = 'firstpage "+ this. ID + "'> homepage </a> <a href ='' id = 'prepage "+ this. ID + "'> previous page </a> ";
If (this. pageno = pagecount) toolbar = toolbar + "the last page of the next page ";
Else toolbar = toolbar + "<a href ='' id = 'nextpage "+ this. ID + "'> next page </a> <a href ='' id = 'lastpage "+ this. ID + "'> last page </a> ";
Toolbar = toolbar + "<input style = 'text-align: center; width: 25px; Height: 20px; Border: 1 solid black 'Type = 'text' id = 'pagesize "+ this. ID + "'value =" + this. pagesize + "/> entries ";
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'> ";
Templatobj.children().html (toolbar );
$ (Templatobj. Wrap ("<div> </div>" pai.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 ('the number of entries you entered is invalid. Please enter it again! ');
$ ("# Pagesize" + thisobj. ID). Focus ();
Return;
}
If (V> 200 ){
Alert ('the number of entries displayed in your input is too large. Please try again! ');
$ ("# 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 ('the number of pages you entered is invalid. Please try again! ');
$ ("# Pageno" + thisobj. ID). Focus ();
Return;
}
If (V> pagecount ){
Alert ('the number of pages you entered exceeds the total number of pages. Please try again! ');
$ ("# Pageno" + thisobj. ID). Focus ();
Return;
}
Thisobj. pageno = V;
Thisobj. Load (thisobj. url, thisobj. Params );
});
}

}< br> // true if the string is empty
var isempty = function (text) {
var isempty = true;
for (VAR I = 0; I If (text. charat (I )! = '') {
isempty = false;
break;
}< BR >}< br> return isempty;
}< br> // 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 = '2016 ';
for (VAR I = 0; 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;
}< BR >}< br> 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.