加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码门户网 (https://www.92codes.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 云计算 > 正文

使用Jquery+Ajax+Json如何实现分页显示附JAVA+JQuery实现异步分

发布时间:2016-12-06 07:45:02 所属栏目:云计算 来源:站长网
导读:先给大家展示下运行效果图: nbsp;1.后台action产生json数据。 List blackList = blackService.getBlackInfoList(mobileNum, gatewayid, startDate, endDate); int totalRows = blackList.size(); StringBuffer sb = new StringBuffer(); sb.append("{"to

/** 
*分页类 
*/ 
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+ "#63;currentPage=" + (this.currentPage + 1)+"");return false;' "; 
    String preClick=" onclick='ajaxpage(""+this.pageURL+ "#63;currentPage=" + (this.currentPage - 1)+"");return false;' "; 
    String firstClick=" onclick='ajaxpage(""+this.pageURL+ "#63;currentPage=1");return false;' "; 
    String lastClick=" onclick='ajaxpage(""+this.pageURL+ "#63;currentPage=" + (this.getPageCount())+"");return false;' "; 
    String onChange=" onchange='ajaxpage(""+this.pageURL+ "#63;currentPage=" + (1)+"");return false;' "; 
    StringBuffer pageString =new StringBuffer(); 
    pageString.append("lt;div class='"+ this.pageStyle +"'gt;lt;span gt;"); 
    // 数字样式 
    if("numberStyle".equalsIgnoreCase(this.pageStyle)) { 
      // 如果只有一页,不需要分页 
      if(this.getPageCount() ==1) { 
        // pageString.append("lt;stronggt; 1lt;/stronggt; "); 
      }else { 
        if(this.currentPage gt;1) {// 如果当前页数大于1,lt;lt; lt;可用 
          pageString.append("lt;a class='pagination-first' "+firstClick+" title='首页' href='" + this.pageURL 
              +"#63;currentPage=1'gt;lt;lt;lt;/agt; "); 
          pageString.append("lt;a class='pagination-prev' "+preClick+"title='上一页' href='" + this.pageURL 
              +"#63;currentPage=" + (this.currentPage -1) 
              +"'gt;lt;lt;/agt; "); 
        }else { 
          pageString 
              .append("lt;a class='pagination-first'gt;lt;lt;lt;/agt; "); 
          pageString 
              .append("lt;a class='pagination-prev'gt;lt;lt;/agt; "); 
        } 
        // 定义向前偏移量 
        intpreOffset = this.currentPage -1 gt; this.pagePreOffset #63;this.pagePreOffset 
            :this.currentPage -1; 
        // 定义向后偏移量 
        intnextOffset = this.getPageCount() -this.currentPage gt;this.pageNextOffset #63;this.pageNextOffset 
            :this.getPageCount() -this.currentPage; 
        // 循环显示链接数字,范围是从 当前页减向前偏移量 到 当前页加向后偏移量 
        for(int i = (this.currentPage - preOffset); i lt;= (this.currentPage + nextOffset); i++) { 
          String numClick=" onclick='ajaxpage(""+this.pageURL+ "#63;currentPage=" + (i)+"");return false;' "; 
          if(this.currentPage == i) {// 当前页要加粗显示 
            pageString 
                .append("lt;strong style='color:black;border:0'gt;" 
                    + i +"lt;/stronggt; "); 
          }else { 
            pageString.append("lt;a "+numClick+"href='"+ this.pageURL 
                +"#63;currentPage=" + i + "'gt;" + i +"lt;/agt; "); 
          } 
        } 
        // 如果当前页小于总页数,gt; gt;gt;可用 
        if(this.currentPage lt;this.getPageCount()) { 
          pageString.append("lt;a class='pagination-next' "+nextClick+" title='下一页' href='" + this.pageURL 
              +"#63;currentPage=" + (this.currentPage +1) 
              +"'gt;gt;lt;/agt; "); 
          pageString.append("lt;a class='pagination-last' "+lastClick+"title='尾页' href='" + this.pageURL 
              +"#63;currentPage=" + (this.getPageCount()) +"'gt;gt;gt;lt;/agt; "); 
        }else { 
          pageString 
              .append("lt;a class='pagination-next' gt;gt;lt;/agt; "); 
          pageString 
              .append("lt;a class='pagination-last'gt;gt;gt;lt;/agt; "); 
        }  
        pageString.append("lt;select id='pageSelect' "+onChange+"gt;"+this.getOptions()+"lt;/selectgt;"); 
      } 
    }else if("normalStyle".equalsIgnoreCase(this.pageStyle)) { 
      if(this.getPageCount() ==1) { 
        pageString.append("lt;stronggt; 共1页lt;/stronggt; "); 
      }else { 
        if(this.currentPage gt;1) { 
          pageString.append("lt;a class='pagination-first' "+firstClick+" title='首页' href='" + this.pageURL 
              +"#63;currentPage=1'gt;lt;lt;lt;/agt; "); 
          pageString.append("lt;a class='pagination-prev' "+preClick+"title='上一页' href='" + this.pageURL 
              +"#63;currentPage=" + (this.currentPage -1) 
              +"'gt;lt;lt;/agt; "); 
        }else { 
          pageString 
            .append("lt;a class='pagination-first'gt;lt;lt;lt;/agt; "); 
          pageString 
            .append("lt;a class='pagination-prev'gt;lt;lt;/agt; "); 
        } 
        pageString.append("lt;span class='pageinfo'gt;第"+this.currentPage+"页/"+this.pageCount+"页lt;/spangt;"); 
        if(this.currentPage lt;this.getPageCount()) { 
          pageString.append("lt;a class='pagination-next' "+nextClick+" title='下一页' href='" + this.pageURL 
              +"#63;currentPage=" + (this.currentPage +1) 
              +"'gt;gt;lt;/agt; "); 
          pageString.append("lt;a class='pagination-last' "+lastClick+"title='尾页' href='" + this.pageURL 
              +"#63;currentPage=" + (this.getPageCount()) +"'gt;gt;gt;lt;/agt; "); 
        }else { 
          pageString 
            .append("lt;a class='pagination-next' gt;gt;lt;/agt; "); 
          pageString 
            .append("lt;a class='pagination-last'gt;gt;gt;lt;/agt; "); 
        } 
        pageString.append("lt;select id='pageSelect' "+onChange+"gt;"+this.getOptions()+"lt;/selectgt;"); 
      } 
    } 
    pageString.append("lt;/spangt;lt;/divgt;"); 
    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 #63; (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("lt;option value=10gt;10lt;/optiongt;lt;option value=20gt;20lt;/optiongt;lt;option value=30gt;30lt;/optiongt;lt;option value=50gt;50lt;/optiongt;lt;option value=100gt;100lt;/optiongt;"); 
      break; 
    case20: 
      sb.append("lt;option value=20gt;20lt;/optiongt;lt;option value=10gt;10lt;/optiongt;lt;option value=30gt;30lt;/optiongt;lt;option value=50gt;50lt;/optiongt;lt;option value=100gt;100lt;/optiongt;"); 
      break; 
    case30: 
      sb.append("lt;option value=30gt;30lt;/optiongt;lt;option value=10gt;10lt;/optiongt;lt;option value=20gt;20lt;/optiongt;lt;option value=50gt;50lt;/optiongt;lt;option value=100gt;100lt;/optiongt;"); 
      break; 
    case50: 
      sb.append("lt;option value=50gt;50lt;/optiongt;lt;option value=10gt;10lt;/optiongt;lt;option value=20gt;20lt;/optiongt;lt;option value=30gt;30lt;/optiongt;lt;option value=100gt;100lt;/optiongt;"); 
      break; 
    case100: 
      sb.append("lt;option value=100gt;100lt;/optiongt;lt;option value=10gt;10lt;/optiongt;lt;option value=20gt;20lt;/optiongt;lt;option value=30gt;30lt;/optiongt;lt;option value=50gt;50lt;/optiongt;"); 
      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#63;
/** 
**  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#63;
/** 
** JS import jquery.js before call function 
*/ 
function ajaxpage(action){ 
  action=action+"perSize="+$("#pageSelect").val(); 
  $.ajax( { 
  type : "POST", 
  url : action, 
  success : function(msg) { 
  //回调函数,后台拼接字符串返回msg 
    //删除原有数据,添加新数据 
    //比如:$("#displayTablegt;thead").nextAll().remove();$("#displayTable").append(msg); 
  } 
  }); 
}

以上就是本文给大家介绍的使用Jquery+Ajax+Json如何实现分页显示附JAVA+JQuery实现异步分页,希望对大家有所帮助。

(编辑:源码门户网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读