转载:JS实现分页功能

转载于https://blog.csdn.net/baidu_41647119/article/details/83479867

为了方便浏览通常将服务器响应回来的大量数据做分页

从实现方式上来说分页基本分为两种方式:

1.查询所有数据返回给客户,通过显示指定区间实现分页,这种方法一般是指JS分页

2.通过客户发送的请求,构造sql语句,查询表中的一段数据。

------------------------------------------------------------------------------------------------------------------

JS实现分页功能代码

html:

<table width="950" cellpadding="0" cellspacing="0" class="table2" align="center">
  
    <tr align="center">
        <th class="td2" height="33" width="150">id</th>
        <th class="td2" >用户名</th>
        <th class="td2" >联系方式</th>
        <th class="td2" >性别</th>
    </tr>


    <tbody id="adminTbody">

    <c:forEach items="${user }" var="user">
    <tr>
    <td>${user.id}</td>
    <td>${user.username}</td>
    <td>${user.tel}</td>
    <td>${user.sex}</td>
   
    </tr>
    </c:forEach>


    </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" οnclick="jumpPage()">跳转</a></li>
            </ul>
        </div>
</div>

js:需要引入jquery

<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>     
 

<script>

$(function(){
    
    goPage(1,10);
    var tempOption="";
    for(var i=1;i<=totalPage;i++)
    {
        tempOption+='<option value='+i+'>'+i+'</option>'
    }
    $("#jumpWhere").html(tempOption);
})
 
 
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=1;i<(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-1;i<endRow;i++)
    {
        $("#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>

css:简单样式

.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;
    }

--------------------------------------------------------------------------------------------------

只要将forEach替换成你的就可以,其他的全部复制粘贴

转载于https://blog.csdn.net/baidu_41647119/article/details/83479867

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值