• 纯js实现分页功能


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

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

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

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

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

    JS实现分页功能代码

    <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-1px 0px 0px 1px; 
            background: #F3F0F0;
            margin-top:25px;
        }
        
        .td0{
            border:#C8C8C8 solid;  
            border-0 0 1px 0;
        }
        
        .td2{
            border:#C8C8C8 solid;   
            border-0 1px 1px 0 ;
        }
        
        .barcon {
             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;
            
        }
     
        .barcon2 a:hover{
            
        }
     
        .ban {
            opacity: .4;
        }

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

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

  • 相关阅读:
    Android 使用Fragment,ViewPagerIndicator 制作csdn app主要框架
    关于activity_main.xml与fragment_main.xml
    在MAC OS 下配置python + Flask ,并支持pyCharm编辑器
    openshift云计算平台diy模式安装Python2.7+Flask
    flask程序部署在openshift上的一些注意事项
    windows下python+flask环境配置详细图文教程
    SegmentFault 2014黑客马拉松 北京 作品demo
    Flask —— 使用Python和OpenShift进行即时Web开发
    支付宝的简单使用
    iOS- 详解文本属性Attributes
  • 原文地址:https://www.cnblogs.com/liuhaotian548/p/13611912.html
Copyright © 2020-2023  润新知