使用Jquery+Ajax+Json实现分页显示的示例分析-成都快上网建站

使用Jquery+Ajax+Json实现分页显示的示例分析

这篇文章将为大家详细讲解有关使用Jquery+Ajax+Json实现分页显示的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联于2013年开始,是专业互联网技术服务公司,拥有项目成都做网站、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元吴兴做网站,已为上家服务,为吴兴各地企业和个人服务,联系电话:13518219792

先给大家展示下运行效果图:

使用Jquery+Ajax+Json实现分页显示的示例分析

 1.后台action产生json数据

List blackList = blackService.getBlackInfoList(mobileNum, gatewayid, startDate, endDate); 
int totalRows = blackList.size(); 
StringBuffer sb = new StringBuffer(); 
     sb.append("{\"totalCount\":\""+totalRows+"\","); 
     sb.append("\"jsonRoot\":["); 
     for (int i=0;i

   2.struts.xml相关配置

 
   
   
  UTF-8 
  /WEB-INF/jsp/manage/black.jsp 
   

3.js获取json数据分页显示

function getJSONData(pn) { 
  // alert(pn); 
  $.getJSON("blackList.ce", function(data) { 
    var totalCount = data.totalCount; // 总记录数 
    var pageSize = 10; // 每页显示几条记录 
    var pageTotal = Math.ceil(totalCount / pageSize); // 总页数 
    var startPage = pageSize * (pn - 1); 
    var endPage = startPage + pageSize - 1; 
    var $ul = $("#json-list"); 
    $ul.empty(); 
    for (var i = 0; i < pageSize; i++) { 
      $ul.append(''); 
    } 
    var dataRoot = data.jsonRoot; 
    if (pageTotal == 1) {   // 当只有一页时 
      for (var j = 0; j < totalCount; j++) { 
        $(".li-tag").eq(j).append("") 
        .append("" + parseInt(j + 1) 
            + "").append("" + dataRoot[j].mobile 
            + "").append("" + dataRoot[j].province 
            + "").append("" + dataRoot[j].gateway 
            + "").append("" + dataRoot[j].insertTime 
            + "").append("" + dataRoot[j].remark 
            + "") 
      } 
    } else { 
      for (var j = startPage, k = 0; j < endPage, k < pageSize; j++, k++) { 
        if( j == totalCount){ 
          break;    // 当遍历到最后一条记录时,跳出循环 
        } 
        $(".li-tag").eq(k).append("") 
        .append("" + parseInt(j + 1) 
            + "").append("" + dataRoot[j].mobile 
            + "").append("" + dataRoot[j].province 
            + "").append("" + dataRoot[j].gateway 
            + "").append("" + dataRoot[j].insertTime 
            + "").append("" + dataRoot[j].remark 
            + "") 
      } 
    } 
    $(".page-count").text(pageTotal); 
  }) 
} 
function getPage() { 
  $.getJSON("blackList.ce", function(data) { 
        pn = 1; 
        var totalCount = data.totalCount; // 总记录数 
        var pageSize = 10; // 每页显示几条记录 
        var pageTotal = Math.ceil(totalCount / pageSize); // 总页数 
        $("#next").click(function() { 
              if (pn == pageTotal) { 
                alert("后面没有了"); 
                pn = pageTotal; 
              } else { 
                pn++; 
                gotoPage(pn); 
              } 
            }); 
        $("#prev").click(function() { 
              if (pn == 1) { 
                alert("前面没有了"); 
                pn = 1; 
              } else { 
                pn--; 
                gotoPage(pn); 
              } 
            }) 
        $("#firstPage").click(function() { 
              pn = 1; 
              gotoPage(pn); 
            }); 
        $("#lastPage").click(function() { 
              pn = pageTotal; 
              gotoPage(pn); 
            }); 
        $("#page-jump").click(function(){ 
          if($(".page-num").val() <= pageTotal && $(".page-num").val() != ''){ 
            pn = $(".page-num").val(); 
            gotoPage(pn); 
          }else{ 
            alert("您输入的页码有误!"); 
            $(".page-num").val('').focus(); 
          } 
        }) 
        $("#firstPage").trigger("click"); 
      }) 
} 
function gotoPage(pn) { 
  // alert(pn); 
  $(".current-page").text(pn); 
  getJSONData(pn) 
} 
$(function() { 
  getPage(); 
})

ps:JAVA+JQuery实现异步分页

最近一个项目要求实现异步分页,简单的写了一下,不好的请指出~

/** 
*分页类 
*/ 
public class PageBean { 
  publicint rowCount = 0; // 总记录数 
  publicint currentPage = 1;// 当前页数 
  publicint sizePerPage = 20;// 每页显示条数 
  publicint pageCount = 0;// 总页数 
  publicString pageURL;// 请求URL 
  publicString pageDisplay;// JSP页面显示 
  publicString pageStyle = "numberStyle";// 分页样式 
  publicint pagePreOffset = 10;// 向前偏移量 
  publicint pageNextOffset = 9;// 向后偏移量 
  publicString pageCss;// 预留 
  publicString getPageCss() { 
    returnpageCss; 
  } 
  publicvoid setPageCss(String pageCss) { 
    this.pageCss = pageCss; 
  } 
  publicString getPageStyle() { 
    returnpageStyle; 
  } 
  publicvoid setPageStyle(String pageStyle) { 
    this.pageStyle = pageStyle; 
  } 
  publicint getPagePreOffset() { 
    returnpagePreOffset; 
  } 
  publicvoid setPagePreOffset(intpagePreOffset) { 
    this.pagePreOffset = pagePreOffset; 
  } 
  publicint getPageNextOffset() { 
    returnpageNextOffset; 
  } 
  publicvoid setPageNextOffset(intpageNextOffset) { 
    this.pageNextOffset = pageNextOffset; 
  } 
  publicString getPageDisplay() { 
    String nextClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=" + (this.currentPage + 1)+"");return false;' "; 
    String preClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=" + (this.currentPage - 1)+"");return false;' "; 
    String firstClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=1");return false;' "; 
    String lastClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=" + (this.getPageCount())+"");return false;' "; 
    String onChange=" onchange='ajaxpage(""+this.pageURL+ "?currentPage=" + (1)+"");return false;' "; 
    StringBuffer pageString =new StringBuffer(); 
    pageString.append(""); 
    // 数字样式 
    if("numberStyle".equalsIgnoreCase(this.pageStyle)) { 
      // 如果只有一页,不需要分页 
      if(this.getPageCount() ==1) { 
        // pageString.append(" 1 "); 
      }else { 
        if(this.currentPage >1) {// 如果当前页数大于1,<< <可用 
          pageString.append("<< "); 
          pageString.append("< "); 
        }else { 
          pageString 
              .append("<< "); 
          pageString 
              .append("< "); 
        } 
        // 定义向前偏移量 
        intpreOffset = this.currentPage -1 > this.pagePreOffset ?this.pagePreOffset 
            :this.currentPage -1; 
        // 定义向后偏移量 
        intnextOffset = this.getPageCount() -this.currentPage >this.pageNextOffset ?this.pageNextOffset 
            :this.getPageCount() -this.currentPage; 
        // 循环显示链接数字,范围是从 当前页减向前偏移量 到 当前页加向后偏移量 
        for(int i = (this.currentPage - preOffset); i <= (this.currentPage + nextOffset); i++) { 
          String numClick=" onclick='ajaxpage(""+this.pageURL+ "?currentPage=" + (i)+"");return false;' "; 
          if(this.currentPage == i) {// 当前页要加粗显示 
            pageString 
                .append("" 
                    + i +" "); 
          }else { 
            pageString.append("" + i +" "); 
          } 
        } 
        // 如果当前页小于总页数,> >>可用 
        if(this.currentPage > "); 
          pageString.append(">> "); 
        }else { 
          pageString 
              .append("> "); 
          pageString 
              .append(">> "); 
        }  
        pageString.append(""+this.getOptions()+""); 
      } 
    }else if("normalStyle".equalsIgnoreCase(this.pageStyle)) { 
      if(this.getPageCount() ==1) { 
        pageString.append(" 共1页 "); 
      }else { 
        if(this.currentPage >1) { 
          pageString.append("<< "); 
          pageString.append("< "); 
        }else { 
          pageString 
            .append("<< "); 
          pageString 
            .append("< "); 
        } 
        pageString.append("第"+this.currentPage+"页/"+this.pageCount+"页"); 
        if(this.currentPage > "); 
          pageString.append(">> "); 
        }else { 
          pageString 
            .append("> "); 
          pageString 
            .append(">> "); 
        } 
        pageString.append(""+this.getOptions()+""); 
      } 
    } 
    pageString.append("
");      this.pageDisplay = pageString.toString();      returnpageDisplay;    }    publicvoid setPageDisplay(String pageDisplay) {      this.pageDisplay = pageDisplay;    }    publicString getPageURL() {      returnpageURL;    }    publicvoid setPageURL(String pageURL) {      this.pageURL = pageURL;    }    publicint getPageCount() {      this.pageCount =this.rowCount %this.sizePerPage ==0 ? (this.rowCount /this.sizePerPage)          : (this.rowCount /this.sizePerPage) +1;      returnthis.pageCount;    }    publicvoid setPageCount(intpageCount) {      this.pageCount = pageCount;    }    publicint getRowCount() {      returnrowCount;    }    publicvoid setRowCount(introwCount) {      this.rowCount = rowCount;    }    publicint getCurrentPage() {      returncurrentPage;    }    publicvoid setCurrentPage(intcurrentPage) {      this.currentPage = currentPage;    }    publicint getSizePerPage() {      returnsizePerPage;    }    publicvoid setSizePerPage(intsizePerPage) {      this.sizePerPage = sizePerPage;    }    privateString getOptions(){      StringBuffer sb =new StringBuffer();      switch(this.sizePerPage) {      case10:          sb.append("10203050100");        break;      case20:        sb.append("20103050100");        break;      case30:        sb.append("30102050100");        break;      case50:        sb.append("50102030100");        break;      case100:        sb.append("10010203050");        break;      }      returnsb.toString();    }  } //后台调用  PageBean page = new PageBean();  setPageInfo(list,request);  public void setPageInfo(List list,HttpServletRequest request){      page.setCurrentPage(this.getCurrentPage());      if(request.getParameter("perSize")==null){        page.setSizePerPage(20);//default 20      }      else{        page.setSizePerPage(Integer.valueOf(request.getParameter("perSize")));      }      page.setRowCount(list.size());      //page.setPageStyle("normalStyle");      //page.setPagePreOffset(5);//default 10      //page.setPageNextOffset(4);//default 9      page.setPageURL(request.getRequestURL().toString());  }  [css] view plaincopyprint? /**  **  CSS  */  .numberStyle,.normalStyle {    text-align:left;  }  .numberStyle a,.normalStyle a {  display: inline-block;  color: #5489F1;   text-decoration: none;  font-size: 14px;  font-weight:bold;  font-family: Geneva, Arial, Helvetica, sans-serif;  border: 1px solid #999;  width: 20px;  height: 20px;  line-height: 20px;  text-align: center;  background-position:center;  }  .numberStyle strong,.normalStyle strong {  display: inline-block;  color: #5489F1;   text-decoration: none;  font-size: 14px;  font-weight:bold;  font-family: Geneva, Arial, Helvetica, sans-serif;  border: 1px solid #999;  width: 20px;  height: 20px;  line-height: 20px;  text-align: center;  background-position:center;  }  .numberStyle a:hover,.normalStyle a:hover{  background-color: #d0d0d0;  }  .normalStyle .pageinfo{    font-size: 14px;    font-family: Geneva, Arial, Helvetica, sans-serif;    color: #5489F1;  }  [javascript] view plaincopyprint? /**  ** JS import jquery.js before call function  */  function ajaxpage(action){    action=action+"&perSize="+$("#pageSelect").val();    $.ajax( {    type : "POST",    url : action,    success : function(msg) {    //回调函数,后台拼接字符串返回msg      //删除原有数据,添加新数据      //比如:$("#displayTable>thead").nextAll().remove();$("#displayTable").append(msg);    }    });  }

关于“使用Jquery+Ajax+Json实现分页显示的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


分享标题:使用Jquery+Ajax+Json实现分页显示的示例分析
链接URL:http://kswjz.com/article/iedhgo.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流