JS-Enhanced Classic pagination example _javascript skills

Source: Internet
Author: User

1 pages displayed:

Copy Code code as follows:

<! DOCTYPE html>

< html>
<title>js_pageusers.html</title>

<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >-->
<script type= "Text/javascript" src= "/js/pageuser.js" ></script>

<body>
<div id= "One" align= "center" >
<div>
User name:
<input type= "text" id= "UserName"/>
Gender:
<input type= "text" id= "Usersex"/>
Occupational:
<input type= "text" id= "Userrole"/>
<br/>
<br/>
<input type= "button" id= "AddUsers" value= "Add User"/>
<input type= "button" id= "Updateusers" value= "Update user"/>
</div>
<br/>
<br/>
<div>
<table border= "1px" cellpadding= "0" cellspacing= "0" width= "500px;" >
<thead>
<th>
User name
</th>
<th>
Gender
</th>
<th>
Occupational
</th>

</thead>


<tbody id= "Showusers" ></tbody>

</table>

<div>
<input type= "button" id= "FirstPage" value= "Home"/>
<input type= "button" id= "BackPage" value= "previous page"/>
<input type= "button" id= "NextPage" value= "next page"/>
<input type= "button" id= "LastPage" value= "Last"/>
<span id= "MSG" ></span>
</div>
</div>


</div>
</body>


2.

Copy Code code as follows:

Window.onload =function () {
var pagesize = 3;//number of records displayed per page
var recondsize = 0;//Total record number
var countpage = 0;//Total Pages
var nowpage= 1;
var users = new Array ()//All records stored

var start = 0; Save the record at the beginning of the current page
var end = 0;//Save the record for the current page

var domusername = $ ("UserName");
var domusersex = $ ("Usersex");
var domuserrole = $ ("userrole");

var domshowusers = $ ("showusers");
var addbtn = $ ("addusers");

Registering Events for buttons
Addbtn.onclick = function () {
Create a User Object
var user = new User (Domusername.value, Domusersex.value,
Domuserrole.value);
Store the user object in an array
Users.push (user);
Recondsize = Users.length; Total number of records obtained
Calculate the total number of pages
Countpage = recondsize% pagesize = = 0? Recondsize/pagesize:math
. ceil (Recondsize/pagesize);

if (Recondsize > PageSize) {//When the total record is greater than the Pagezie train of thought from the back 3 number
start = Recondsize-pagesize;
End=recondsize;
}else{//pagesize start = 1;
start=0;
End=recondsize; The actual number of end= is recondsize
}

Invoke method to display user
Showuser (users,start,end,recondsize,countpage,domshowusers);

}
Get page-related buttons
var firstpage = $ ("FirstPage");
var backPage = $ ("BackPage");
var nextPage = $ ("NextPage");
var lastpage = $ ("LastPage");

Firstpage.onclick = function () {
Nowpage = 1;

if (recondsize<= pagesize && recondsize > 0) {
start = 0;
end = Recondsize;
}else{
start=0;
End=pagesize;
}

Showuser (users,start,end,recondsize,countpage,domshowusers);

}


Backpage.onclick = function () {
Nowpage = nowpage-1;//Re-assign value

if (nowpage<=1) {
nowpage=1;
}

if (recondsize <= pagesize && recondsize > 0) {
start = 0;
end = Recondsize;
}else{
Start= (nowpage-1) *pagesize;
End = (nowpage-1) *pagesize+pagesize;
}

Showuser (users,start,end,recondsize,countpage,domshowusers);

}

Nextpage.onclick = function () {
Nowpage = nowpage+1;//Re-assign value

if (nowpage>=countpage) {
Nowpage=countpage;
}

if (recondsize <= pagesize && recondsize > 0) {
start = 0;
end = Recondsize;
}else{
Start= (nowpage-1) *pagesize;
if ((nowpage-1) *pagesize+pagesize>=recondsize) {
end = Recondsize;
}else{
End = (nowpage-1) *pagesize+pagesize;
}
}
Showuser (users,start,end,recondsize,countpage,domshowusers);

}
Lastpage.onclick = function () {
Nowpage = countpage;//Re-assign value

if (recondsize <= pagesize && recondsize > 0) {
start = 0;
end = Recondsize;
}else{
Start= (nowpage-1) *pagesize;
end = Recondsize;
}
Showuser (users,start,end,recondsize,countpage,domshowusers);

}

}

To create a function function that holds the user object data
function User (name, sex, role) {
THIS.name = name;
This.sex = sex;
This.role = role;
}

function $ (ID) {
return document.getElementById (ID);
}

function Showuser (users,start,end,recondsize,countpage,domshowusers) {

Emptying data
for (var JJ = 0; JJ < domshowUsers.childNodes.length;) {
Domshowusers.removechild (DOMSHOWUSERS.CHILDNODES[JJ]);
}

For Loop added
for (Var i=start;i<end;i++) {
var user = users[i];//Consider
Create a row
var tr = document.createelement ("tr");
Creating columns
var TD1 = document.createelement ("TD");
Create a text node
var td1textnode = document.createTextNode (User.Name);
Text node added to TD
Td1.appendchild (Td1textnode);

Creating columns
var td2 = document.createelement ("TD");
Create a text node
var td2textnode = document.createTextNode (user.sex);
Text node added to TD
Td2.appendchild (Td2textnode);

Creating columns
var td3 = document.createelement ("TD");
Create a text node
var td3textnode = document.createTextNode (user.role);
Text node added to TD
Td3.appendchild (Td3textnode);

To add a column to a row
Tr.appendchild (TD1);
Tr.appendchild (TD2);
Tr.appendchild (TD3);

Add Rows to Tbody
if (Domshowusers.haschildnodes ()) {
Domshowusers.insertbefore (Tr,domshowusers.firstchild)//Add data before last data
}else{
Domshowusers.appendchild (TR);//Add to end
}
}
document.getElementById ("msg"). InnerHTML = "Total: {" + recondsize
+ "} Records, total {" + Countpage + "} page";
}

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.