Add Table data on the JQuery page and implement pagination

Source: Internet
Author: User

Add Table data on the JQuery page and implement pagination

Use JQuery to add and pagination table data on the page. For more information, see.

The Code is as follows:

Var countPage;

Var nowPag = 1;

Var pageSize;

Var countSize;

 

Var starIndex;

Var endIndex;

 

// Information submitted by the user

Var name;

Var sex;

Var age;

 

// Define the row number

Var num = 1;

 

$ (Document). ready (function (){

// Register an event for adding a user

$ ("# Submit"). click (function (){

// Obtain information submitted by the user

Name = $ ("# name"). val ();

Sex = $ ("input [name = 'sex ']: checked"). val ();

Age = $ ("# age"). val ();

PageSize = $ ("# selectSize option: selected"). val ();

// Alert (name + sex + age + pageSize );

 

// Create the tr object in the table

$ Tr = $ ("<tr class = 'data'> </tr> ");

$ Td1 =$ ("<td> </td> ");

$ Td2 = $ ("<td> </td> ");

$ Td3 =$ ("<td> </td> ");

$ Td4 = $ ("<td> </td> ");

$ Td5 = $ ("<td> </td> ");

 

$ Tr. append ($ td1.append ("<input type = 'checkbox'> "));

$ Tr. append ($ td2.append (name ));

$ Tr. append ($ td3.append (sex ));

$ Tr. append ($ td4.append (age ));

$ Tr. append ($ td5.append ("<input type = 'button 'value = 'delete'> "));

 

$ ("# Show"). append ($ tr );

PageNation ();

 

});

// Register the operation that selects the number of displayed items

$ ("# SelectSize"). change (function (){

PageSize = $ ("# selectSize option: selected"). val ();

PageNation ();

});

 

// Register the button click event for the paging operation

$ ("# First"). click (pageNation );

$ ("# Back"). click (pageNation );

$ ("# Next"). click (pageNation );

$ ("# Last"). click (pageNation );

 

});

 

// Paging Functions

Var pageNation = function (){

// Obtain the number of all data entries

CountSize = $ ("# show tr"). size ();

// Obtain the total number of pages

CountPage = Math. ceil (countSize/pageSize );

 

// Handle page flip operations

If (this. nodeType = 1 ){

Var idValue = $ (this). attr ("id ");

If ("first" = idValue ){

// Alert (idValue );

NowPag = 1;

} Else if ("back" = idValue ){

// Alert (nowPag );

If (nowPag> 1 ){

NowPag --;

}

 

} Else if ("next" = idValue ){

// Alert (idValue );

If (nowPag <countPage ){

NowPag ++;

}

} Else if ("last" = idValue ){

// Alert (idValue );

NowPag = countPage;

}

 

}

// Alert (pageSize );

// Obtain the subscript of the start and end of the display.

StarIndex = (nowPag-1) * pageSize + 1;

EndIndex = nowPag * pageSize;

 

If (endIndex> countSize ){

// Alert ("subscript greater than the total number of records" + endIndex );

EndIndex = countSize;

}

 

If (countSize <pageSize ){

// Alert ("the total number of records is smaller than the number of entries displayed on each page" + endIndex );

EndIndex = countSize;

}

 

// Alert (starIndex );

 

If (starIndex = endIndex ){

// Displayed operation

$ ("# Show tr: eq (" + (starIndex-1) + ")"). show ();

$ ("# Show tr: lt (" + (starIndex-1) + ")"). hide ();

} Else {

// Displayed operation

$ ("# Show tr: gt (" + (starIndex-1) + ")"). show ();

$ ("# Show tr: lt (" + (endIndex-1) + ")"). show ();

 

// Hidden operations

$ ("# Show tr: lt (" + (starIndex-1) + ")"). hide ();

$ ("# Show tr: gt (" + (endIndex-1) + ")"). hide ();

}

// Change the display at the bottom

$ ("# SizeInfo ")

. Html (

"From" + starIndex + "to" + endIndex + ", total" + countSize

+ "Records .");

$ ("# PageInfo" ).html ("current" + nowPag + "Page, total" + countPage + "page .");

};

 

 

[Html] view plaincopy view CODE snippets derived from my CODE snippets on CODE

 

<! DOCTYPE html>

<Html>

<Head>

<Title> implemented with jquery </title>

 

<Meta name = "keywords" content = "keyword1, keyword2, keyword3">

<Meta name = "description" content = "this is my page">

<Metaname = "content-type" content = "text/html; charset = UTF-8">

<Script type = "text/javascript" src = ".../js/jquery-2.0.3.min.js"> </script>

<! -- <Link rel = "stylesheet" type = "text/css" href = "./styles.css"> -->

<Style type = "text/css">

Div {

Border: 1px black solid;

}

 

# TableDiv {

Height: 500px;

}

 

# InsertDiv {

Width: 300px;

Margin-right: 550px;

}

 

# TableDiv {

Width: 500px;

Margin-left: 350px;

}

 

# Top {

Width: 500px;

Height: 400px;

}

 

# TopTable, # contentTable, # bottomTable {

Width: pixel PX;

}

</Style>

 

 

<Script type = "text/javascript" src = "../js/jqueryTablePageNation. js"> </script>

</Head>

 

<Body>

<Div id = "content" align = "center">

<Form action = "">

 

<Div id = "insertDiv" style = "width: 263px;">

<Table id = "insertData" border = "1px">

<Tr>

<Td> name <input type = "text" id = "name" value = "donghogyu"> </td>

</Tr>

<Tr>

<Td> gender <input type = "radio" name = "sex" value = "male"

Checked = "checked"> male <input type = "radio" name = "sex"

Value = "female"> female

</Td>

 

</Tr>

<Tr>

<Td> age <input type = "text" id = "age" value = "21"> </td>

</Tr>

<Tr>

<Td align = "center"> <input type = "button" id = "submit"

Value = "add data"> </td>

</Tr>

</Table>

</Div>

 

<Div id = "tableDiv">

<Div id = "top">

<Table id = "topTable" border = "1px">

<Thead>

 

<Th> <input type = "checkbox"> select all </th>

<Th> name </th>

<Th> gender </th>

<Th> password </th>

<Th> operation </th>

 

</Thead>

<Tbody id = "show">

 

</Tbody>

</Table>

</Div>

 

<Div id = "bottom">

<Table id = "bottomTable" border = "1px">

<Tr align = "center">

<Td> <input type = "button" value = "Homepage" id = "first"> </td>

<Td> <input type = "button" value = "Previous Page" id = "back"> </td>

<Td> <input type = "button" value = "next" id = "next"> </td>

<Td> <input type = "button" value = "last" id = "last"> </td>

<Td> <select id = "selectSize">

<Option value = "3"> 3 </option>

<Option value = "5"> 5 </option>

<Option value = "10"> 10 </option>

</Select> entries </td>

</Tr>

<Tr align = "center">

<Td colspan = "6"> <span id = "sizeInfo"> currently, there are 0 to 0th records. </span> </td>

</Tr>

<Tr align = "center">

<Td colspan = "6"> <span id = "pageInfo"> the current page is 0th, 0 in total. </span> </td>

</Tr>

</Table>

 

</Div>

</Div>

 

 

</Form>

</Div>

</Body>

</Html>

 

 

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.