Use ajax to implement paging and deletion (2)

Source: Internet
Author: User


After all the information in the database can be traversed and displayed, the page is displayed on the JSP page.

// Add nodes implemented by PAGE

FunctionPagesnode (pageshtmlnode, rootnode ){

// Obtain the current page

VaRNowpage = rootnode. getattribute ("nowpage ");

// Obtain the total number of pages

VaRCountpage = rootnode. getattribute ("countpage ");

// Obtain the total number of records

VaRCountsize = rootnode. getattribute ("countsize ");

 

VaRMsgnode = Document. createtextnode ("current" + nowpage + "Page, total"

+ Countpage + "Page, total" + countsize + "record ");

// Clearing method

Clearnodes (pageshtmlnode );

VaRFirstpage = Document. createelement ("");

Firstpage. setattribute ("href ","#");

Firstpage. appendchild (document. createtextnode ("Homepage "));

Firstpage. onclick =Function(){

Nowpage = 1;

// Query information on the current page

Getpagesinfo (nowpage );

}

Pageshtmlnode. appendchild (firstpage );

VaRBackpage = Document. createelement ("");

Backpage. setattribute ("href ","#");

Backpage. appendchild (document. createtextnode ("Previous Page "));

Backpage. onclick =Function(){

Nowpage = eval (nowpage + "-" + 1 );

If(Nowpage <= 1 ){

Nowpage = 1;

}

//// Query Information on the current page

Getpagesinfo (nowpage );

}

Pageshtmlnode. appendchild (backpage );

VaRNextpage = Document. createelement ("");

Nextpage. setattribute ("href ","#");

Nextpage. appendchild (document. createtextnode ("next page "));

Nextpage. onclick =Function(){

Nowpage = eval (nowpage + "+" + 1 );

If(Nowpage> = countpage ){

Nowpage = countpage;

}

// Query information on the current page

Getpagesinfo (nowpage );

}

Pageshtmlnode. appendchild (nextpage );

VaRLastpage = Document. createelement ("");

Lastpage. setattribute ("href ","#");

Lastpage. appendchild (document. createtextnode ("last page "));

Lastpage. onclick =Function(){

Nowpage = countpage;

// Query information on the current page

Getpagesinfo (nowpage );

}

Pageshtmlnode. appendchild (lastpage );

Pageshtmlnode. appendchild (msgnode );

}

 

 

// Paging code

FunctionGetpagesinfo (nowpage ){

// Modify chkshtmlnode to the default value

VaRChkshtmlnode = getnode ("Chk ");

Chkshtmlnode. Checked =Null;

 

VaRPageshtmlnode = getnode ("pages ");

VaREmpshtmlnode = getnode ("EMPs ");

// XML data passed from the server is parsed here

// Step 1: Create an XMLHTTPRequest object

VaRXMLHTTP = createxmlhttprequest ();

// Clear

Clearnodes (empshtmlnode );

XMLHTTP. onreadystatechange =Function(){

If(XMLHTTP. readystate = 4 ){

If(XMLHTTP. Status = 200 ){

// Obtain xmldocument

VaRXmldoc = XMLHTTP. responsexml;

// Obtain the tag

VaRRootnode = xmldoc.doc umentelement;

// Obtain all EMP element nodes in the XML file

VaREMPs = xmldoc. getelementsbytagname ("EMP"); // EMP id = "XXX"

// Obtain all child nodes in the EMP Node

For(VaRI = 0; I <EMPs. length; I ++ ){

// Obtain a specific EMP:

VaREmpnode = EMPs [I];

// Create a row for EMP

VaRTr = Document. createelement ("TR ");

// Create the ID TD

VaRTD1 = Document. createelement ("TD ");

Td1.appendchild (document. createtextnode (EMPs [I]

. Getattribute ("ID ")));

Tr. appendchild (TD1 );

VaRElementnodes = empnode. childnodes; // name, sex, age

For(VaRJ = 0; j <elementnodes. length; j ++ ){

// Whether the node is an element node

If(Elementnodes [J]. nodetype = 1 ){

VaRTd2 = Document. createelement ("TD ");

Td2

. Appendchild (document

. Createtextnode (elementnodes [J]. firstchild. nodevalue ));

Tr. appendchild (td2 );

}

}

VaRDelchk = Document. createelement ("input ");

Delchk. setattribute ("type", "checkbox ");

Delchk. setattribute ("name", "delchk ");

Delchk

. Setattribute ("value", EMPs [I]

. Getattribute ("ID "));

Tr. appendchild (delchk );

Empshtmlnode. appendchild (TR );

Empshtmlnode. appendchild (TR );

}

// Call the page to create a Node object related to the page

Pagesnode (pageshtmlnode, rootnode );

}

}

}

// Step 2: Specify the Request Parameters

XMLHTTP. Open ("get", "./employeeservlet? Nowpage = "+ nowpage

+ "& Timestamp =" +NewDate (). gettime (),
True);

// Step 3: send the request

XMLHTTP. Send (Null);

}

In this case, compared with the previous servlet, the servlet actually transfers several more parameters.

Add the parameters to be passed before the for loop.

Out. println ("<? Xmlversion = \ "1.0 \" encoding = \ "UTF-8 \"?> ");

// Transfer the total number of records on the current page to the EMPs and tag

Out. println ("<empsnowpage = '" + Page. getnowpage () + "'countsize = '" + Page. getcountsize () + "'countpage = '" + Page. getcountpage () + "'> ");

For (INTI = 0; I <EMPs. Size (); I ++ ){

Employeeemp = EMPs. Get (I );

Out. println ("<empid = '" + EMP. GETID () + "'> ");

Out. println ("<Name>" + EMP. gethrname () + "</Name> ");

Out. println ("<sex>" + EMP. gethrsex () + "</sex> ");

Out. println ("<age>" + EMP. gethrage () + "</age> ");

Out. println ("<birth>" + EMP. gethrbirth () + "</birth> ");

Out. println ("<salary>" + EMP. gethrsalary () + "</salary> ");

Out. println ("</EMP> ");

}

Out. println ("</EMPs> ");

Of course, you must obtain and judge the parameters before passing them.

Stringnowpage = request. getparameter ("nowpage ");

System. Out. println ("========" + nowpage );

Intnowpage = 1;

If (nowpage = NULL ){

Nowpage = 1;

} Else {

Nowpage = integer. parseint (nowpage );

}

 

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.