主要是借鉴了网上一个例子,修改了一些小地方,前端分页的技巧,表格的数据是已经写好了,可以前端渲染表格然后再分页,都是可以的。
其实分页最关键是这两句:
var startRow = (currentPage - 1) * pageSize+1 //currentPage 为当前页,pageSize为每页显示的数据量
var endRow = currentPage * pageSize
找到我们需要显示的行的范围(starRow~endRow)
ps:这里在跳转的时候遇到了一个小BUG,就是获取到的select的value值是string类型的,比如获取到了1,然后你想再加1的时候就会变成"11" 而不是我们想要的"2",所以这里需要用parseInt( )来转换一下,小细节需要注意呀!!!
效果图:
[javascript] view plain copy print?
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<style type="text/css">
a{
text-decoration: none
}
.table2{
border:#C8C8C8 solid
border-width:1px 0px 0px 1px
background: #F3F0F0
margin-top:25px
}
.td0{
border:#C8C8C8 solid
border-width:0 0 1px 0
}
.td2{
border:#C8C8C8 solid
border-width:0 1px 1px 0
}
.barcon {
width: 1000px
margin: 0 auto
text-align: center
}
.barcon1 {
font-size: 17px
float: left
margin-top: 20px
}
.barcon2 {
float: right
}
.barcon2 ul {
margin: 20px 0
padding-left: 0
list-style: none
text-align: center
}
.barcon2 li {
display: inline
}
.barcon2 a {
font-size: 16px
font-weight: normal
display: inline-block
padding: 5px
padding-top: 0
color: black
border: 1px solid #ddd
background-color: #fff
}
.barcon2 a:hover{
background-color: #eee
}
.ban {
opacity: .4
}
</style>
</head>
<body>
<table width="950" cellpadding="0" cellspacing="0" class="table2" align="center">
<thead>
<tr>
<td colspan="3" height="33" class="td0"> </td>
<td align="center" class="td2"><a href="###">添加用户</a></td>
</tr>
<tr align="center">
<th width="150" height="33" class="td2">序号</th>
<th width="300" class="td2">用户名</th>
<th width="250" class="td2">权限</th>
<th width="250" class="td2">操作</th>
</tr>
</thead>
<tbody id="adminTbody">
<tr align="center">
<td class="td2" height="33" width="150">1</td>
<td class="td2" >admin</td>
<td class="td2" >管理员</td>
<td class="td2" ><a href="###">修改</a></td>
</tr>
</tbody>
</table>
<div id="barcon" class="barcon" >
<div id="barcon1" class="barcon1"></div>
<div id="barcon2" class="barcon2">
<ul>
<li><a href="###" id="firstPage">首页</a></li>
<li><a href="###" id="prePage">上一页</a></li>
<li><a href="###" id="nextPage">下一页</a></li>
<li><a href="###" id="lastPage">尾页</a></li>
<li><select id="jumpWhere">
</select></li>
<li><a href="###" id="jumpPage" onclick="jumpPage()">跳转</a></li>
</ul>
</div>
</div>
<script src="jquery.js"></script>
<script>
/*动态生成用户函数
num为生成的用户数量
*/
function dynamicAddUser(num){
for(var i=1i<=numi++)
{
var trNode=document.createElement("tr")
$(trNode).attr("align","center")
//序号
var tdNodeNum=document.createElement("td")
$(tdNodeNum).html(i+1)
tdNodeNum.style.width = "150px"
tdNodeNum.style.height = "33px"
tdNodeNum.className = "td2"
//用户名
var tdNodeName=document.createElement("td")
$(tdNodeName).html("lzj"+i)
tdNodeName.style.width = "300px"
tdNodeName.className = "td2"
//权限
var tdNodePri=document.createElement("td")
tdNodePri.style.width = "250px"
tdNodePri.className = "td2"
var priText=document.createElement("span")
$(priText).css({"display":"inline-block","text-align":"center"})
$(priText).text("普通用户")
tdNodePri.appendChild(priText)
//操作
var tdNodeOper = document.createElement("td")
tdNodeOper.style.width = "170px"
tdNodeOper.className = "td2"
var editA = document.createElement("a")
$(editA).attr("href", "###").text("编辑")
$(editA).css({ display: "inline-block" })
tdNodeOper.appendChild(editA)
trNode.appendChild(tdNodeNum)
trNode.appendChild(tdNodeName)
trNode.appendChild(tdNodePri)
trNode.appendChild(tdNodeOper)
$("#adminTbody")[0].appendChild(trNode)
}
}
$(function(){
dynamicAddUser(80)
goPage(1,10)
var tempOption=""
for(var i=1i<=totalPagei++)
{
tempOption+='<option value='+i+'>'+i+'</option>'
}
$("#jumpWhere").html(tempOption)
})
/**
* 分页函数
* pno--页数
* psize--每页显示记录数
* 分页部分是从真实数据行开始,因而存在加减某个常数,以确定真正的记录数
* 纯js分页实质是数据行全部加载,通过是否显示属性完成分页功能
**/
var pageSize=0//每页显示行数
var currentPage_=1//当前页全局变量,用于跳转时判断是否在相同页,在就不跳,否则跳转。
var totalPage//总页数
function goPage(pno,psize){
var itable = document.getElementById("adminTbody")
var num = itable.rows.length//表格所有行数(所有记录数)
pageSize = psize//每页显示行数
//总共分几页
if(num/pageSize > parseInt(num/pageSize)){
totalPage=parseInt(num/pageSize)+1
}else{
totalPage=parseInt(num/pageSize)
}
var currentPage = pno//当前页数
currentPage_=currentPage
var startRow = (currentPage - 1) * pageSize+1
var endRow = currentPage * pageSize
endRow = (endRow > num)? num : endRow
//遍历显示数据实现分页
/*for(var i=1i<(num+1)i++){
var irow = itable.rows[i-1]
if(i>=startRow && i<=endRow){
irow.style.display = ""
}else{
irow.style.display = "none"
}
}*/
$("#adminTbody tr").hide()
for(var i=startRow-1i<endRowi++)
{
$("#adminTbody tr").eq(i).show()
}
var tempStr = "共"+num+"条记录 分"+totalPage+"页 当前第"+currentPage+"页"
document.getElementById("barcon1").innerHTML = tempStr
if(currentPage>1){
$("#firstPage").on("click",function(){
goPage(1,psize)
}).removeClass("ban")
$("#prePage").on("click",function(){
goPage(currentPage-1,psize)
}).removeClass("ban")
}else{
$("#firstPage").off("click").addClass("ban")
$("#prePage").off("click").addClass("ban")
}
if(currentPage<totalPage){
$("#nextPage").on("click",function(){
goPage(currentPage+1,psize)
}).removeClass("ban")
$("#lastPage").on("click",function(){
goPage(totalPage,psize)
}).removeClass("ban")
}else{
$("#nextPage").off("click").addClass("ban")
$("#lastPage").off("click").addClass("ban")
}
$("#jumpWhere").val(currentPage)
}
function jumpPage()
{
var num=parseInt($("#jumpWhere").val())
if(num!=currentPage_)
{
goPage(num,pageSize)
}
}
</script>
</body>
</html>
原则上像这样的功能,应该直接在asp中完成,而不是在前端通过js来实现,这纯属脱裤子放屁。
下面是修改后的代码,不再需要js:
<i id="pronumber"><%for i = 1 to n%><a href="<%=filename%>?page=<%=i%>&<%=url%>"><span<%if i=int(request("page")) then%>style="background:redcolor:#fff"<%end if%>><%=i%></span></a><%next%></i>
另外,html标签的id是具有唯一性的,也就是说每个标签的id是不能相同的。如果出现相同,那么只有第一个才能被js找到。你的这些span都用了相同的id,所以样式始终被匹配到第一个span中。这就是你的代码的错误所在。此种情况应该用class而不是用id,class(类)是允许重复的。
无刷新分页代码,jQuery分页完整示例,分页形式经典,兼容性也做的好,网页上的分页代码<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />
<title>jQuery分页演示效果</title>
<script type="text/javascript" src="/img/jquery1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var show_per_page = 5
var number_of_items = $('#content').children().size()
var number_of_pages = Math.ceil(number_of_items/show_per_page)
$('#current_page').val(0)
$('#show_per_page').val(show_per_page)
var navigation_html = '<a class="previous_link" href="javascript:previous()">Prev</a>'
var current_link = 0
while(number_of_pages >current_link){
navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>'
current_link++
}
navigation_html += '<a class="next_link" href="javascript:next()">Next</a>'
$('#page_navigation').html(navigation_html)
$('#page_navigation .page_link:first').addClass('active_page')
$('#content').children().css('display', 'none')
$('#content').children().slice(0, show_per_page).css('display', 'block')
})
function previous(){
new_page = parseInt($('#current_page').val()) - 1
if($('.active_page').prev('.page_link').length==true){
go_to_page(new_page)
}
}
function next(){
new_page = parseInt($('#current_page').val()) + 1
//if there is an item after the current active link run the function
if($('.active_page').next('.page_link').length==true){
go_to_page(new_page)
}
}
function go_to_page(page_num){
var show_per_page = parseInt($('#show_per_page').val())
start_from = page_num * show_per_page
end_on = start_from + show_per_page
$('#content').children().css('display', 'none').slice(start_from, end_on).css('display', 'block')
$('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page')
$('#current_page').val(page_num)
}
</script>
<style>
#page_navigation a{
padding:3px
border:1px solid gray
margin:2px
color:black
text-decoration:none
}
.active_page{
background:darkblue
color:white !important
}
</style>
</head>
<body>
<input type='hidden' id='current_page' />
<input type='hidden' id='show_per_page' />
<div id='content'>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Vestibulum consectetur ipsum sit amet urna euismod imperdiet aliquam urna laoreet.</p>
<p>Curabitur a ipsum ut elit porttitor egestas non vitae libero.</p>
<p>Pellentesque ac sem ac sem tincidunt euismod.</p>
<p>Duis hendrerit purus vitae nibh tincidunt bibendum.</p>
<p>Nullam in nisi sit amet velit placerat laoreet.</p>
<p>Vestibulum posuere ligula non dolor semper vel facilisis orci ultrices.</p>
<p>Donec tincidunt lorem et dolor fringilla ut bibendum lacus fringilla.</p>
<p>In non eros eu lacus vestibulum sodales.</p>
<p>Duis ultrices metus sit amet sem adipiscing sit amet blandit orci convallis.</p>
<p>Proin ullamcorper est vitae lorem mollis bibendum.</p>
<p>Maecenas congue fringilla enim, tristique laoreet tortor adipiscing eget.</p>
<p>Duis imperdiet metus et lorem venenatis nec porta libero porttitor.</p>
<p>Maecenas lacinia lectus ac nulla commodo lacinia.</p>
<p>Maecenas quis massa nisl, sed aliquet tortor.</p>
<p>Quisque porttitor tellus ut ligula mattis luctus.</p>
<p>In at mi dolor, at consectetur risus.</p>
<p>Etiam id erat ut lorem fringilla dictum.</p>
<p>Curabitur sagittis dolor ac nisi interdum sed posuere tellus commodo.</p>
<p>Pellentesque quis magna vitae quam malesuada aliquet.</p>
<p>Curabitur tempus tellus quis orci egestas condimentum.</p>
<p>Maecenas laoreet eros ac orci adipiscing pharetra.</p>
<p>Nunc non mauris eu nibh tincidunt iaculis.</p>
<p>Ut semper leo lacinia purus hendrerit facilisis.</p>
<p>Praesent et eros lacinia massa sollicitudin consequat.</p>
<p>Proin non mauris in sem iaculis iaculis vel sed diam.</p>
<p>Nunc quis quam pulvinar nibh volutpat aliquet eget in ante.</p>
<p>In ultricies dui id libero pretium ullamcorper.</p>
<p>Morbi laoreet metus vitae ipsum lobortis ultrices.</p>
<p>Donec venenatis egestas arcu, quis eleifend erat tempus ullamcorper.</p>
<p>Morbi nec leo non enim mollis adipiscing sed et dolor.</p>
<p>Cras non tellus enim, vel mollis diam.</p>
<p>Phasellus luctus quam id ligula commodo eu fringilla est cursus.</p>
<p>Ut luctus augue tortor, in volutpat enim.</p>
<p>Cras bibendum ante sed erat pharetra sodales.</p>
<p>Donec sollicitudin enim eu mi suscipit luctus posuere eros imperdiet.</p>
<p>Vestibulum mollis tortor quis ipsum suscipit in venenatis nulla fermentum.</p>
<p>Proin vehicula suscipit felis, vitae facilisis nulla bibendum ac.</p>
<p>Cras iaculis neque et orci suscipit id porta risus feugiat.</p>
<p>Suspendisse eget tellus purus, ac pulvinar enim.</p>
<p>Morbi hendrerit ultrices enim, ac rutrum felis commodo in.</p>
<p>Suspendisse sagittis mattis sem, sit amet faucibus nisl fermentum vitae.</p>
<p>Nulla sed purus et tellus convallis scelerisque.</p>
<p>Nam at justo ut ante consectetur faucibus.</p>
<p>Proin dapibus nisi a quam interdum lobortis.</p>
<p>Nunc ornare nisi sed mi vehicula eu luctus mauris interdum.</p>
<p>Mauris auctor suscipit tellus, at sodales nisi blandit sed.</p>
</div>
<div id='page_navigation'></div><br>
<br>
</body>
</html>