Ajax-based paging Query

Source: Internet
Author: User

First, add several divs on the page:

<Div id = "div_trackpoint" style = "border: solid 1px gray; Height: 230px; width: 99%; overflow-Y: auto; "> used to display the queried data </div>
<Div id = "div_trackpages" style = "height: 15px; width: 99%; font-size: 8pt; Word-break: Break-all; Word-wrap: Break-word; "> display the page number </div> <br/>
<Div id = "div_trackpagetab" style = "height: 15px; width: 99%; font-size: 8pt; text-align: center; "> used to display the previous, next ,..., home page, end page, and other control buttons </div>
<Div id = "div_trackpage_status" style = "height: 20px; width: 99%; font-size: 8pt; text-align: center; "> display the page number of the current page </div>

Implement the client paging function:
VaR curpage = 0; // current page
VaR totalpage = 0; // total number of pages
VaR pagetab = 7; // The number of pages displayed in each group
VaR curtab = 0; // current Group

My program is to set 50 data entries per page and 7 pages per group, which can be adjusted as needed.

The turntab function is used to switch a group and calculate which page is displayed based on the input Val. Val = 1 indicates switching to the next group, val =-1 indicates switching to the previous group, 0 indicates switching to the first group, and-2 indicates switching to the last group. You can click the... symbol on the page to switch the group.

Function turntab (VAL)
{
VaR npage = 0;

If (val = 1 ){
Curtab ++;
Npage = (curtab-1) * pagetab + 1;
}
Else if (val =-1 ){
Curtab --;
Npage = (curtab-1) * pagetab + 1;
}
Else if (val = 0 ){
Curtab = 1;
Npage = 1;
}
Else if (val =-2 ){
Curtab = math. Floor (totalpage/pagetab) + 1;
Npage = (curtab-1) * pagetab + 1;
}

VaR carinfo = Document. getelementbyid ("div_trackpages ");
VaR tabinfo = Document. getelementbyid ("div_trackpagetab ");

VaR strinner = "";

Strinner + = "<a href =" "javascript: turnpage (1)" "> homepage </a> & nbsp ;";

Strinner + = "<a href =" "javascript: previouspage ()"> previous page </a> & nbsp ;";

Strinner + = "Total" + totalpage + "Page & nbsp ;";

Strinner + = "<a href =" "javascript: nextpage ()" "> next page </a> & nbsp ;";

Strinner + = "<a href =" "javascript: turnpage (" + totalpage + ")" "> last page </a> & nbsp ;";

Tabinfo. innerhtml = strinner;
Strinner = "";

If (curtab> 1) strinner + = "<a href =" "javascript: turntab (-1)">... </a> & nbsp ;";

For (; npage <= curtab * pagetab; npage ++ ){

If (npage <= totalpage ){

Strinner + = "<a href =" "javascript: turnpage (" + npage + ")"> "+ npage +" </a> & nbsp ;"

}
}

If (npage <totalpage) strinner + = "<a href =" "javascript: turntab (1)">... </a> & nbsp ;";

Carinfo. innerhtml = strinner;

}

The turnpage function is used to switch pages. Val indicates the number of pages to switch to. query ranges are generated based on the number of pages to be displayed. For example, the first 50 records, from 51 to 100, are queried, records from 101 to 150...
Csccustomanalyst is an asynchronous call function. The code for function implementation will be pasted below. "Method", "Sid", "time1", "time2", "row1", "row2" are all required parameters for query execution. method is used to determine what query to execute, "row1", "row2" indicates the range of records to be queried. Other parameters are adjusted as needed. Showcartrack (VAL) is a function that processes the query results. The result obtained after the asynchronous call is completed is analyzed and displayed in this function. This part of the code is written according to the specific implementation, which is not listed here.

Function turnpage (VAL)
{
If (number (VAL )! = Curpage ){

Curpage = Number (VAL );

VaR row1 = string (curpage-1) * 50 + 1 );
VaR row2 = string (curpage * 50 );
VaR trackinfo = Document. getelementbyid ("div_trackpoint ");
Trackinfo. innerhtml = "retrieving data. Please wait ...";

_ Csccustomanalyst (["method", "Sid", "time1", "time2", "row1", "row2"],
["Getcartrack", "80100117", T1, T2, row1, row2],
Showcartrack, onqueryerror );

If (curpage = 1) turntab (0 );

If (curpage = totalpage) turntab (-2 );

VaR statusinfo = Document. getelementbyid ("div_trackpage_status ");

Statusinfo. innerhtml = "page" + curpage + "page ";
}

}

Nextpage switches to the next page and calls turnpage. If the next page exceeds the current group, switch to the next group.

Function nextpage ()
{
If (curpage <totalpage ){

Turnpage (curpage + 1 );

If (curpage + 1)> (curtab * pagetab )){
Turntab (1 );
}
}

}

Previouspage switches to the previous page and calls turnpage. If the previous page exceeded the current group, switch to the previous group.
Function previouspage ()
{
If (curpage> 1 ){

Turnpage (CurPage-1 );

If (curpage-1) <= (curtab-1) * pagetab )){
Turntab (-1 );
}

}
}

_ Csccustomanalyst is an asynchronous call function, xhr. Open ("Post", "mapquery. ashx", true); this section indicates that the request is submitted to the mapquery. ashx page. All server segment database operations are performed in mapquery. ashx.

Function _ csccustomanalyst (paramnames, paramvalues, oncomplete, onerror ){

VaR xhr = _ getxmlhttprequest ();

Xhr. Open ("Post", "mapquery. ashx", true );

Xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");

Xhr. onreadystatechange = function (){

VaR readystate = xhr. readystate;

If (readystate = 4 ){

VaR status = xhr. status;

If (status = 200 ){
VaR resultset = xhr. responsetext;

If (resultset = NULL ){
Oncomplete (null );
Return;
}

If (oncomplete ){
Oncomplete (resultset );
Resultset = NULL;
}
}
Else {
If (onerror ){
Onerror (xhr. responsetext );
}
}
Xhr. onreadystatechange = function (){};

Xhr = NULL;
}
};

VaR paramstring = NULL;

If (paramnames & paramnames. length> 0 ){

VaR Params = new array ();

While (paramnames & paramnames. length> 0)
{
Params. Push (paramnames. Shift () + "=" + _ tojson (paramvalues. Shift ()));
}

Paramstring = Params. Join ("&");

}

Xhr. Send (paramstring );
};

Finally, the SQL statement used to query an Oracle database by quantity range is listed: (query the first 50 records)
Select *
From (select/* + full (tablename) */fieldname, rownum rn from tablename where condition and rownum <= 50 order by field DESC) T2 where t2.rn> = 1;

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.