js 表格分页,ajax请求后台数据前台分页

$(function(){
	
		var url="后台给的地址";
			var  shuju=document.getElementById("shuju");
			console.log(shuju);
			var lc=$("#lc").val();
			var zy=$("#zy").val();
			var ys=$("#ys").val();
			var rw=$("#rw").val();
			var ls=$("#ls").val();
			var fq=$("#fq").val();
			var list="";
		
	$('.shuju').html('<img src="./img/ajax-loader-1.gif"/>').css('text-align','cente');
						$.ajax({
							type:"get",
							url:url,
							data:{
								TA006:lc,
								TA034:zy,
								TA035:ys,
								ProduceNo:rw,
								ProcessName:ls,
								WorkerName:fq
								
							},
							dataType:"json",
							success:function(res){
								console.log(res);
								console.log("请求成功");
							for(key in res){
//拼接循环表格数据
								list+='<tr calss="hang"><td>'+res[key].ProduceNo+'</td><td >'+res[key].ProduceNo+'</td><td >'+res[key].ProcessName+'</td><td >'+res[key].TA006+'</td><td >'+res[key].TA034+'</td><td >'+res[key].TA035+'</td><td >'+res[key].WorkerName+'</td></tr>';
//						console.log(res[key].ProduceNo);
						
			
							 }
							shuju.innerHTML=list;//显示数据
							
							goPage(1,10);//调用分页
				},
//				
							 error: function(XMLHttpRequest, textStatus, errorThrown) {
									console.log(404);
							   }
					});
		});
//
//分页
window.onload=function(){ 

}
function goPage(pno,psize){
    var mytable = document.getElementById("table");
    var num = mytable.rows.length;//表格所有行数(所有记录数)
//  console.log(num);
    var totalPage = 0;//总页数
    var pageSize = psize;//每页显示行数
    //总共分几页 
    if(num/pageSize > parseInt(num/pageSize)){   
            totalPage=parseInt(num/pageSize)+1;   
       }else{   
           totalPage=parseInt(num/pageSize);   
       }   
    var currentPage = pno;//当前页数
    var startRow = (currentPage - 1) * pageSize+1;//开始显示的行  31 
       var endRow = currentPage * pageSize;//结束显示的行   40
       endRow = (endRow > num)? num : endRow;    40
//     console.log(endRow);
       //遍历显示数据实现分页
    for(var i=2;i<(num+1);i++){    
        var irow = mytable.rows[i-1];
        if(i>=startRow && i<=endRow){
            irow.style.display = "";    
        }else{
            irow.style.display = "none";
        }
    }
    var pageEnd = document.getElementById("pageEnd");
    var tempStr = '<p class="show-text"><span>共'+num+'条记录</span> <span>分'+totalPage+'页</span> <span>当前第'+currentPage+'页</span></P>';
    if(currentPage>1){
        tempStr += "<a class='page-btn'style='text-decoration:none;'  href=\"#\" onClick=\"goPage("+(1)+","+psize+")\">首页</a>";
        tempStr += "<a class='page-btn'style='text-decoration:none;'  href=\"#\" onClick=\"goPage("+(currentPage-1)+","+psize+")\"><上一页</a>"
    }else{
//第一页禁用
        tempStr += "<a class='page-btn btnfirst' style='border-color:#ccc;color:#ccc;text-decoration:none;'>首页</a>";
        tempStr += "<a class='page-btn btnfirst' style='border-color:#ccc;color:#ccc;text-decoration:none;'><上一页</a>";    
    }

    if(currentPage<totalPage){
        tempStr += "<a class='page-btn'style='text-decoration:none; ' href=\"#\" onClick=\"goPage("+(currentPage+1)+","+psize+")\">下一页></a>";
        tempStr += "<a class='page-btn'style='text-decoration:none; ' href=\"#\" onClick=\"goPage("+(totalPage)+","+psize+")\">尾页</a>";
    }else{
//到最后一页禁用
        tempStr += "<a class='page-btn btnlast' style='border-color:#ccc;color:#ccc;text-decoration:none;' >下一页</a>";
        tempStr += "<a class='page-btn btnlast' style='border-color:#ccc;color:#ccc;text-decoration:none;' >尾页</a>"; 
        
    }

    document.getElementById("barcon").innerHTML = tempStr;
    
}

  

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <link rel="stylesheet" href="css/bootstrap.min.css" />
        <link rel="stylesheet" href="css/pagination.css" />
        <style type="text/css">
            table td{
                /*width:100px!important;*/
                padding:3px 0!important;
            }
            table tr{
            }
            .table-box{
                overflow-y: auto;
                   max-height: 300px;
            }
            .biaodan .row{
                text-align: center;
            }
            .biaodan .col-sm-4{
                margin-top: 10px;
            }
            .btn-box{
                text-align: center;
                margin-top: 10px;
            }
            .btn-box button{
                margin-right: 10px;
            }
            .content{
                height:300px;
            }
            .page{
                margin:0 auto;
                text-align: center;
            }
            .shuju{
                text-align: center;
                margin:0 auto;
            }
            /*.load-img{
                text-align: center;
                display: block;
                margin:0 auto;    
            }*/
            thead th,thead td{
                text-align: center!important;
                vertical-align: middle!important;
            }
            .table tr td{
                    width:30px;
            }
            tbody td{
            
                text-align: left!important;
                vertical-align: middle!important;
            }
            .on{
                background: #428BCA;
            }
            
            /*.page nav{
                text-align: center!important;
            }*/
            /*.page ul{
                display: inline-block;
                text-align: center;
            }*/
            .page-btn{
                display: inline-block;
                width: 70px;
                height: 25px;
                border: 1px solid #0062CC;
                text-decoration: none;
                border-radius: 5px;
                margin-right: 10px;
                line-height: 25px;
                
            }
        .page-wrap{
            text-align: center;
            background: #cfccff;
            line-height: 36px;
            height: 36px;
        }
            .page-btn:hover{
                background: lightskyblue;
                color: #fff
            }
            .show-text{
                float: left;
            }
            .show-text span{
                margin-right: 10px;
            }
        </style>
        
    </head>
    <body>
        <header>
            <div class="head">
                <ul class="nav nav-tabs">
                  <!--<li role="presentation" class="active"><a href="#">Home</a></li>
                  <li role="presentation"><a href="#">Profile</a></li>
                  <li role="presentation"><a href="#">Messages</a></li>-->
                </ul>
            </div>
        </header>
        <div class="container">
            <div class="biaodan">
            <form action="" method="post">
                <div class="row">
                    <div class="col-sm-4">
                        <span id='1'>流程名:</span>
                        <input type="text" value=""  id="lc"/>
                    </div>
                    <div class="col-sm-4">
                        <span id="hgfg">摘要信息:</span>
                        <input type="text" value="" name="hhh" id="zy"/>
                    </div>
                    <div class="col-sm-4">
                        <span>优先级:</span>
                        <input type="text" /value="" id="ys"/>
                    </div>
                    <div class="col-sm-4">
                        <span>任务号:</span>
                        <input type="text" value="" name="index" id="index"/>
                    </div>
                    <div class="col-sm-4">
                        <span>&nbsp;&nbsp;&nbsp;&nbsp;流水号:</span>
                        <input type="text" value="" id="ls"/>
                    </div>
                    <div class="col-sm-4">
                        <span>发起人:</span>
                        <input type="text" value="" id="fq"/>
                    </div>
                </div>
                <p class="btn-box">
                    <button type="button" class="btn btn-primary" Id="search">查询</button>
                    <button type="button" class="btn btn-primary">重置</button>
                </p>
                
            </form>
        </div>
        </div>
        <div class="container">
            <div class="table-box">
                <table class="table table-bordered" id="table">
                    <thead>
                        <tr>
                            <th>操作enen</th>
                            <th>任务号</th>
                            <th>流水号</th>
                            <th>流程名</th>
                            <th>摘要信息</th>
                            <th>优先级</th>
                            <th>发起人</th>
                        </tr>
                    </thead>
                    <tbody class="shuju" id="shuju">
                        
                        //在js,ajax请求里的那个拼接,获取后台数据
                        
                        <!--<tr calss="hang">
                            <td>1</td>
                            <td>23</td>
                            <td>11</td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>-->
                    </tbody>
                </table>
            </div>
        </div>
    <div class="page-wrap">
        <div id="barcon" name="barcon"></div>
    </div>
        <script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
        <script type="text/javascript" src="js/bootstrap.min.js"></script>
        <!--<script src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/paging.js">
            
        </script>-->
        <!--<script type="text/javascript" src="js/mui.min.js"></script>-->
        <script type="text/javascript">
            $('.load-img').html('<img src="img/ajax-loader.gif"/>').css('text-align','cente');
        </script>
        <script src="js/my.js"></script>
    </body>
</html>

 

posted @ 2017-05-18 20:08  车水码龙  阅读(440)  评论(0编辑  收藏  举报