DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js加強的經典分頁實例
js加強的經典分頁實例
編輯:關於JavaScript     

1顯示的頁面:
復制代碼 代碼如下:

<!DOCTYPE html>

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

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is 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>
</head>

<body>
<div id="one" align="center">
<div>
用戶名:
<input type="text" id="userName" />
性別:
<input type="text" id="userSex" />
職業:
<input type="text" id="userRole" />
<br />
<br />
<input type="button" id="addUsers" value="添加用戶" />
<input type="button" id="updateUsers" value="更新用戶" />
</div>
<br />
<br />
<div>
<table border="1px" cellpadding="0" cellspacing="0" width="500px;">
<thead>
<th>
用戶名
</th>
<th>
性別
</th>
<th>
職業
</th>

</thead>


<tbody id="showUsers"></tbody>

</table>

<div>
<input type="button" id="firstPage" value="首頁"/>
<input type="button" id="backPage" value="上一頁"/>
<input type="button" id="nextPage" value="下一頁"/>
<input type="button" id="lastPage" value="末頁"/>
<span id="msg"></span>
</div>
</div>


</div>
</body>

</html>


2.
復制代碼 代碼如下:
window.onload =function(){
var pagesize = 3;//每頁顯示的記錄數
var recondsize = 0;//總記錄數
var countpage = 0;//總頁數
var nowpage= 1;
var users = new Array();//存放所有的記錄

var start = 0; //保存當前頁開始的記錄
var end = 0 ;//保存當前頁結束的記錄

var domUserName = $("userName");
var domUserSex = $("userSex");
var domUserRole = $("userRole");

var domshowUsers = $("showUsers");
var addBtn = $("addUsers");

//為按鈕注冊事件
addBtn.onclick = function() {
//創建user對象
var user = new User(domUserName.value, domUserSex.value,
domUserRole.value);
//把user對象存放數組中
users.push(user);
recondsize = users.length; //得出總記錄數
//計算出總頁數
countpage = recondsize % pagesize == 0 ? recondsize / pagesize : Math
.ceil(recondsize / pagesize);

if (recondsize > pagesize) { //當總記錄大於pagezie 思路 從後往前數3個數
start = recondsize-pagesize;
end=recondsize;
}else{ // pagesize start =1;
start=0;
end=recondsize; //end=實際的個數 就是 recondsize
}

 //調用顯示user的方法
 showUser(users,start,end,recondsize,countpage,domshowUsers);

}
 //獲取分頁相關的按鈕
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;//重新賦值

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;//重新賦值

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;//重新賦值

if (recondsize <= pagesize && recondsize > 0) {
start = 0;
end = recondsize;
}else{
 start=(nowpage-1)*pagesize;
 end = recondsize;
}
showUser(users,start,end,recondsize,countpage,domshowUsers);

}

}

//創建一個Function函數 函數是保存User對象數據的
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){

//清空數據
for ( var jj = 0; jj < domshowUsers.childNodes.length;) {
domshowUsers.removeChild(domshowUsers.childNodes[jj]);
}

 //for循環添加的
 for(var i=start;i<end;i++){
 var user = users[i];//考慮
 //創建一行
var tr = document.createElement("tr");
//創建列
var td1 = document.createElement("td");
//創建文本節點
var td1TextNode = document.createTextNode(user.name);
//文本節點添加到td中
td1.appendChild(td1TextNode);

//創建列
var td2 = document.createElement("td");
//創建文本節點
var td2TextNode = document.createTextNode(user.sex);
//文本節點添加到td中
td2.appendChild(td2TextNode);

//創建列
var td3 = document.createElement("td");
//創建文本節點
var td3TextNode = document.createTextNode(user.role);
//文本節點添加到td中
td3.appendChild(td3TextNode);

//把列添加到行中
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);

//把行添加到tbody中
if(domshowUsers.hasChildNodes()){
domshowUsers.insertBefore(tr,domshowUsers.firstChild);//再最後一個數據之前添加數據
}else{
 domshowUsers.appendChild(tr);//添加到末尾
}
}
 document.getElementById("msg").innerHTML = "總共:{" + recondsize
+ "}條記錄,共{" + countpage + "}頁";
}

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved