json一次取所有数据,然后在前后进行分页显示

来源:互联网 发布:mac 储存文件 编辑:程序博客网 时间:2024/05/16 13:48

  var mJson = null;
  var pageIndex = 1;//当前页号
  var pageCount = 0;//总页号
  var pageSize  = 20;//每页显示数
  
  //查询后返回值处理
  function update_page(result) {
   mJson = result;
   var mSumRec   = mJson.length;
    pageIndex  = 1;
    pageCount  = Math.floor((mSumRec -1)/pageSize) + 1;
   setPageNo();
   dealPage();
  }
     //每次操作时设置显示总页数与当前页数
     function setPageNo(){
      if (pageCount == 0){
       $("#mSum").html("0");
      }else{
       $("#mSum").html(pageCount);
      }
      $("#mCurr").html(pageIndex);
     }
     //首页
  function topPage(){
   pageIndex = 1;
   setPageNo();
   dealPage();
  }
  //上一页
  function previousPage(){
   if (pageIndex > 1){
    pageIndex = pageIndex -1;
   }else{
    pageIndex = 1;
   }
   setPageNo();
   dealPage();
  }
  //下一页
  function nextPage(){
   if (pageCount > pageIndex){
    pageIndex = pageIndex + 1;
   }else{
    pageIndex = pageCount;
   }
   setPageNo();
   dealPage();
  }
  //最后一页
  function bottomPage(){
   pageIndex = pageCount;
   setPageNo();
   dealPage();
  }
  //分组显示收放
  function show(){
   var obj2 = document.getElementById("imgs");
   if (document.getElementById("dtree").style.display == ""){
    obj2.src = "/images/right.gif";
    document.getElementById("dtree").style.display = "none";
    document.getElementById("dtreeTd").style.width = "0px";
   }else{
    obj2.src = "/images/left.gif";     
    document.getElementById("dtreeTd").style.width = document.getElementById("dtree").style.width;
    document.getElementById("dtree").style.display = "";
   }
  }
  //打印
  function printInfo() {
   
         var oldInfo = document.body.outerHTML;
         document.body.innerHTML = document.getElementById('bak').outerHTML;
        //window.document.getElementById("bak").outerHTML;
         window.print();
         //wb.execwb(7,1);打印预览
         document.body.innerHTML = oldInfo;
  }
  //获取页面数据
  function getPageSize(){  
    var xScroll, yScroll;  
    if (window.innerHeight && window.scrollMaxY) {  
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else{ // Explorer Macwould also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {  // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }  
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
      pageHeight = windowHeight;
    } else { 
      pageHeight = yScroll;
    }
    if(xScroll < windowWidth){  
      pageWidth = windowWidth;
    } else {
      pageWidth = xScroll;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,xScroll,yScroll) 
    return arrayPageSize;
   //return pageHeight;
  }

 

function opera(groupId,leafNod,groupParent){
      $.ajax({
     url : "/HrbSystemT/manageDriver.do?command=QIntSort",
     type : "post",
     dataType : "json",
     data : {"groupId":groupId,"groupParent":groupParent},
     success : update_page
    });
     }
     //每次分页后处理
     function dealPage(){
   $("#table1>tr").remove();
      if (mJson != null && mJson.length > 0){
       var mMax =  pageIndex*pageSize;
       if (pageIndex == pageCount){
        mMax = mJson.length;
       }
    for(var i=(pageIndex-1)*pageSize; i<mMax; i++){
      var tr = $("<tr></tr>");
     var td = $("<td></td>");
     var td1 = $("<td></td>");
     td.append(mJson[i].driverName);
     td1.append(mJson[i].intEgral);
     tr.append(td);
     tr.append(td1);
     $("#table1").append(tr);
    }
    document.getElementById("dtree").style.height = getPageSize()[5]-25+"px";
   }
     }

 

 

jsp:

<div id="bak" >
  <table class=table1 width="100%" border=1 cellspacing="0" cellpadding="3" bordercolordark="#ffffff" bordercolorlight="#003366">
   <tr>
    <td  class=td1  bgcolor="#ebebeb" align="center">司机姓名</td>
    <td class=td1  bgcolor="#ebebeb" align="center">积分</td>
   </tr>
   <tbody id= "table1"></tbody>
  </table>
  <table   width="100%" border="0">
   <tr>
        <td  nowrap class="rd19" height="2" width="36%"> <div align="left"><font color="#666666">&nbsp;总页数:&nbsp;</font><font color="#666666" id="mSum"></font><font color="#666666">&nbsp;页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当前页号:</font>&nbsp;<font color="#666666" id="mCurr"></font></div></td>
        <td  nowrap class="rd19" width="64%"> <div align="right">
         <a href="javaScript:topPage()">首 页</a>&nbsp;&nbsp;
         <a href="javaScript:previousPage()">上一页</a>&nbsp;&nbsp;
         <a href="javaScript:nextPage()">下一页</a>&nbsp;&nbsp;
         <a href="javaScript:bottomPage()">最后一页</a>&nbsp;&nbsp;</div>
        </td>
      </tr>
  </table>
 </div>