js分頁頁碼,js分頁
<1>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server"> <title>使用AJAX分頁</title> <script src="jquery-1.11.2.js" type="text/javascript"></script> <style type="text/css"> table{ margin:80px 600px; } td{ width:50px; height:auto} div{ width:50px; height:30px; border:1px solid Black; background-color:lavender; line-height:30px ; text-align:center} #txtPage{ width:25px; height:25px; line-height:25px; margin:0px; } .style1 { height: 36px; } </style> <script type="text/javascript"> var currentPage = 1; //當前頁 var pageCount = 0; //總頁數。目前=0然後會在getPageData裡重新給pageCount賦值。得到一個真正的總頁數 var getPageData = function (currentPage) { $.get("API/GetPageData.ashx?currentPage=" + currentPage + "", function (obj) { //假設當前頁是第二頁currentPage=2 var arr = obj.split("|"); var JsonData = arr[0]; //擷取Json格式的字串資料 JsonData = $.parseJSON(JsonData); //將字串轉換成Json對象 //debugger; for (var i = 0; i < JsonData.length; i++) { var data = "<tr><td >" + JsonData[i].Locid + "</td><td >" + JsonData[i].LocName + "</td><td >" + JsonData[i].ParentId + "</td><td >" + JsonData[i].LocType + "</td><td >" + JsonData[i].ElongCode + "</td><td >" + JsonData[i].CityCode + "</td><td >" + JsonData[i].BaiduPos + "</td><td >" + JsonData[i].Versions + "</td></tr>"; $("#t1").append(data); } //-----------------下面做頁碼標籤 pageCount = arr[1]; //擷取總頁數 var getDivAll = $("#t2 div"); //擷取所有div //---給div取名字(賦值) if (currentPage == 1); //當 當前頁為第一頁的時候的處理方式 { $.each(getDivAll, function (key, val) { if (key == 0) { $(this).text("返回"); } else { $(this).text(key); } if (key == getDivAll.length - 1) { $(this).text("下一頁"); } if (key == getDivAll.length - 2) { $(this).html("<input type='text'id=txtPage />/100頁"); } if (key == getDivAll.length - 3) { $(this).text("..." + pageCount); } }) } if (currentPage != 1 && currentPage <= 6) { //當 當前頁小於7並且不等於1的時候的處理方式 $.each(getDivAll, function (key, val) { if (key == 0) { $(this).text("返回"); } else { $(this).text(key - 1); } if (key == 1) { $(this).text("<<"); } if (key == getDivAll.length - 1) { $(this).text("下一頁"); } if (key == getDivAll.length - 2) { $(this).html("<input type='text'id=txtPage />/100頁"); } if (key == getDivAll.length - 3) { $(this).text("..." + pageCount); } }) } var i = 5; //這個i=5的作用:假如但前頁大於第6頁的時候,頁碼標籤從當前頁減去i開始顯示。然後這個i減去1 //例如 當前頁是第十頁,那麼10-5=5 那麼頁碼標籤從5開始顯示 即顯示5 6 7 8 9 10 11 12 13 14 if (currentPage > 6 && currentPage <= pageCount) { //當 當前頁大於6切小於等於最後一頁的時候的處理方式 $.each(getDivAll, function (key, val) { if (key == 0) { $(this).text("返回"); } else { $(this).text(currentPage - i); i = i - 1; } if (key == 1) { $(this).text("<<"); } if (key == getDivAll.length - 1) { $(this).text("下一頁"); } if (key == getDivAll.length - 2) { $(this).html("<input type='text'id=txtPage />/100頁"); } if (key == getDivAll.length - 3) { $(this).text("..." + pageCount); } }) } }) } //---------------------------------當點擊div元素引發事件。 $(function () { $("#t2 div").click(function () { $("#t1 tr:not(:eq(0))").remove(); //在點擊div觸發事件後,載入資料的時候先清空原始的資料(第一行資料除外) if ($(this).text() == "<<") { getPageData(parseInt(currentPage) - 1); //調用 getPageData()方法,參數是當前頁減1(這裡做了資料轉換) currentPage = currentPage - 1; //將請當前頁更新(即:當前頁從原來的數值更新為減去1後的值了) } else if ($(this).text() == "返回") { getPageData(1); currentPage = 1; } else if ($(this).text() == "..." + pageCount) { getPageData(pageCount); currentPage = pageCount; } else if ($(this).text() == "下一頁") { getPageData(parseInt(currentPage) + 1) currentPage = currentPage + 1; } else { currentPage = $(this).text(); getPageData(parseInt(currentPage)); } }) }) </script></head><body onload="getPageData(1)"> <table border="1" cellpadding="3px" cellspacing="0px" style="margin-top:50px; margin-left:50px ;width:700px;" id="t1"> <tr><td>編號</td><td >城市名</td><td >父ID</td><td >locType</td><td >elongCode</td><td >CityCode</td><td >BaiduPos</td><td >Version</td></tr> </table> <table style="margin-top:50px; margin-left:50px" id="t2"> <tr> <td class="style1"><div></div></td><td class="style1"><div id="DisyDiv"></div></td><td class="style1"><div></div></td> <td class="style1"><div></div></td><td class="style1"><div></div></td><td class="style1"><div></div></td> <td class="style1"><div></div></td><td class="style1"><div></div></td><td class="style1"><div></div></td> <td class="style1"><div></div></td><td class="style1"><div></div></td><td class="style1"><div></div></td> <td class="style1"><div></div></td><td class="style1"><div style="width:75px; padding:0px"></div></td><td class="style1"><div></div></td> </tr> </table> </body></html>