The ajax space and paging functions used in the SQL Business Edition are very good for learning their js Code.
// Ajax Control
Function PageAjax (){
Var xhrObj = null;
If (window. XMLHttpRequest)
{
XhrObj = new XMLHttpRequest ();
}
Else if (window. ActiveXObject)
{
Try {
XhrObj = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (e1)
{
Try {
XhrObj = new ActiveXObject ("MSXML2.XMLHTTP ");
}
Catch (e2 ){
Try {
XhrObj = new ActiveXObject ("MSXML3.XMLHTTP ");
}
Catch (e3 ){
Alert ("failed to Create Ajax:" + e3)
}
}
}
}
Else
{
Alert ("Unrecognized browsers ");
}
Return xhrObj;
}
Function Page (curPage, labelid, classid, installdir, url, refreshtype, specialid)
{
This. labelid = labelid;
This. classid = classid;
This. url = url;
If (labelid. substring (0, 5) = "{SQL _")
{
Var slabelid = labelid. split (') [0];
Slabelid = slabelid. replace ("{","");
This. c_obj = "c _" + slabelid;
This. p_obj = "p _" + slabelid;
}
Else
{
This. c_obj = "c _" + labelid;
This. p_obj = "p _" + labelid;
}
This. installdir = installdir;
This. refreshtype = refreshtype;
This. specialid = specialid;
This. page = curPage;
LoadData (1 );
}
Function loadData (p)
{This. page = p;
Var xhr = new PageAjax ();
Xhr. open ("get", installdir + url + "? Labelid = "+ escape (labelid) + "& classid =" + classid + "& refreshtype =" + refreshtype + "& specialid =" + specialid + "& curpage =" + p, true );
Xhr. onreadystatechange = function (){
If (xhr. readyState = 1)
{
If (p = 1)
Eval ('document. all. '+ c_obj ). innerHTML = "<div align = 'center'> connecting to the server... </div> ";
}
Else if (xhr. readyState = 2 | xhr. readyState = 3)
{
If (p = 1)
Eval ('document. all. '+ c_obj ). innerHTML = "<div align = 'center'> reading data... </div> ";
}
Else if (xhr. readyState = 4)
{
If (xhr. status = 200)
{
Var pagearr = xhr. responseText. split ("{ks: page }")
Var pageparamarr = pagearr [1]. split ("| ");
Count = pageparamarr [0];
Perpagenum = pageparamarr [1];
Pagecount = pageparamarr [2];
Itemunit = pageparamarr [3];
Itemname = pageparamarr [4];
Pagestyle = pageparamarr [5];
GetObject (c_obj). innerHTML = pagearr [0];
Pagelist ();
}
}
}
Xhr. send (null );
}
Function getObject (id)
{
If (document. getElementById)
{
Return document. getElementById (id );
}
Else if (document. all)
{
Return document. all [id];
}
Else if (document. layers)
{
Return document. layers [id];
}
}
Function pagelist ()
{
Var n = 1;
Var statushtml = null;
Switch (parseInt (this. pagestyle ))
{
Case 1:
Statushtml = "Total" + this. count + this. itemunit + "<a href = \" javascript: homePage (1); \ "title = \" homePage \ "> homePage </a> <a href = \" javascript: previousPage () \ "title = \" Previous Page \ "> previous page </a> <a href = \" javascript: nextPage () \ "title = \" Next Page \ "> next page </a> <a href = \" javascript: lastPage (); \ "title = \" Last Page \ "> last page </a>: <font color = red>" + this. page + "</font>/" + this. pagecount + "page" + this. perpagenum + this. itemunit + this. itemname + "/page ";
Break;
Case 2:
Statushtml = "<a href = '#'>" + this. pagecount + "Page/" + this. count + this. itemunit + "</a> <a href = \" javascript: homePage (1 ); \ "title = \" homepage \ "> <font face = webdings> 9 </font> </a> <a href = \" javascript: previousPage () \ "title = \" Previous Page \ "> <font face = webdings> 7 </font> </a> ";
Var startpage = 1;
If (this. page = 10)
Startpage = 2;
Else if (this. page> 10)
Startpage = eval (parseInt (this. page/10)-1) * 10 + parseInt (this. page) % 10) + 2 );
For (var I = startpage; I <= this. pagecount; I ++ ){
If (I = this. page)
Statushtml + = "<a href = \" # \ "> <font color = \" # ff0000 \ ">" + I + "</font> </a>"
Else
Statushtml + = "<a href = \" javascript: turn ("+ I +") \ ">" + I + "</a>"
N = n + 1;
If (n> 10) break;
}
Statushtml + = "<a href = \" javascript: nextPage () \ "title = \" Next Page \ "> <font face = webdings> 8 </font> </a> <a href = \" javascript: lastPage (); \ "title = \" Last Page \ "> <font face = webdings >:</font> </a> ";
Statushtml = "<span class = 'kspage'>" + statushtml + "</span> ";
Break;
Case 4:
Statushtml = "Total" + this. pagecount + "Page/" + this. count + this. itemunit + this. itemname + "<a href = \" javascript: homePage (1 ); \ "title = \" homepage \ "> <font face = webdings> 9 </font> </a> <a href = \" javascript: previousPage () \ "title = \" Previous Page \ "> <font face = webdings> 7 </font> </a> ";
For (var I = this. page; I <= this. pagecount; I ++ ){
If (I = this. page)
Statushtml + = "<B> <font color = \" # ff0000 \ ">" + I + "</font> </B>"
Else
Statushtml + = "<a href = \" javascript: turn ("+ I +") \ "> [" + I + "] </a>"
N = n + 1;
If (n> 10) break;
}
Statushtml + = "<a href = \" javascript: nextPage () \ "title = \" Next Page \ "> <font face = webdings> 8 </font> </a> <a href = \" javascript: lastPage (); \ "title = \" Last Page \ "> <font face = webdings >:</font> </a> ";
Break;
Case 3:
Statushtml = "No. <font color = # ff000>" + this. page + "</font> page total" + this. pagecount + "page <a href = \" javascript: homePage (1 ); \ "title = \" homepage \ "> </a> <a href = \" javascript: previousPage () \ "title = \" Previous Page \ "> </a> <a href = \" javascript: nextPage () \ "title = \" Next Page \ ">></a> <a href = \" javascript: lastPage (); \ "title = \" Last Page \ ">></a>" + this. perpagenum + this. itemunit + this. itemname + "/page ";
Break;
}
Statushtml + = "No. <select name = \" goto \ "onchange = \" turn (parseInt (this. value); \ "> ";
For (var I = 1; I <= this. pagecount; I ++ ){
If (I = this. page)
Statushtml + = "<option value = '" + I + "'selected>" + I + "</option> ";
Else
Statushtml + = "<option value = '" + I + "'>" + I + "</option> ";
}
Statushtml + = "</select> page ";
GetObject (this. p_obj). innerHTML = statushtml;
}
Function homePage ()
{
If (this. page = 1)
Alert ("It's already the homepage! ")
Else
LoadData (1 );
}
Function lastPage ()
{
If (this. page = this. pagecount)
Alert ("this is the last page! ")
Else
LoadData (this. pagecount );
}
Function previousPage ()
{
If (this. page> 1)
LoadData (this. page-1 );
Else
Alert ("already the first page ");
}
Function nextPage ()
{
If (this. page <this. pagecount)
LoadData (this. page + 1 );
Else
Alert ("the last page has been reached ");
}
Function turn (I)
{
LoadData (I );
}