使用Jquery+Ajax+Json如何實(shí)現(xiàn)分頁顯示附JAVA+JQuery實(shí)現(xiàn)異步分頁
先給大家展示下運(yùn)行效果圖:
1.后臺(tái)action產(chǎn)生json數(shù)據(jù)。
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<blackList.size();i++) { LBlack blackInfo = (LBlack)blackList.get(i); sb.append("{\"id\":\""+ blackInfo.getId()); sb.append("\","); sb.append("\"mobile\":\""+ blackInfo.getMobile()); sb.append("\","); sb.append("\"province\":\""+ blackInfo.getProvince()); sb.append("\","); sb.append("\"gateway\":\""+ blackInfo.getGateway()); sb.append("\","); sb.append("\"insertTime\":\""+ blackInfo.getInsertTime()); sb.append("\","); sb.append("\"remark\":\""+ blackInfo.getRemark()); sb.append("\""); sb.append("},"); } sb.deleteCharAt(sb.lastIndexOf(",")); // 刪去最后一個(gè)逗號(hào) sb.append("]}"); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/plain"); response.getWriter().print(sb);
2.struts.xml相關(guān)配置
<action name="blackList" class="blackAction" method="blackList"> <!--plaintext用于顯示頁面原始代碼的結(jié)果類型--> <result type="plainText"> <param name="charSet">UTF-8</param> <param name="location">/WEB-INF/jsp/manage/black.jsp</param> </result> </action>
3.js獲取json數(shù)據(jù)分頁顯示
function getJSONData(pn) { // alert(pn); $.getJSON("blackList.ce", function(data) { var totalCount = data.totalCount; // 總記錄數(shù) var pageSize = 10; // 每頁顯示幾條記錄 var pageTotal = Math.ceil(totalCount / pageSize); // 總頁數(shù) 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('<li class="li-tag"></li>'); } var dataRoot = data.jsonRoot; if (pageTotal == 1) { // 當(dāng)只有一頁時(shí) for (var j = 0; j < totalCount; j++) { $(".li-tag").eq(j).append("<span class='col1'><input type='checkbox' value='"+parseInt(j + 1)+"'/></span>") .append("<span class='col2'>" + parseInt(j + 1) + "</span>").append("<span class='col3'>" + dataRoot[j].mobile + "</span>").append("<span class='col4'>" + dataRoot[j].province + "</span>").append("<span class='col5'>" + dataRoot[j].gateway + "</span>").append("<span class='col6'>" + dataRoot[j].insertTime + "</span>").append("<span class='col7'>" + dataRoot[j].remark + "</span>") } } else { for (var j = startPage, k = 0; j < endPage, k < pageSize; j++, k++) { if( j == totalCount){ break; // 當(dāng)遍歷到最后一條記錄時(shí),跳出循環(huán) } $(".li-tag").eq(k).append("<span class='col1'><input type='checkbox' value='"+parseInt(j + 1)+"'/></span>") .append("<span class='col2'>" + parseInt(j + 1) + "</span>").append("<span class='col3'>" + dataRoot[j].mobile + "</span>").append("<span class='col4'>" + dataRoot[j].province + "</span>").append("<span class='col5'>" + dataRoot[j].gateway + "</span>").append("<span class='col6'>" + dataRoot[j].insertTime + "</span>").append("<span class='col7'>" + dataRoot[j].remark + "</span>") } } $(".page-count").text(pageTotal); }) } function getPage() { $.getJSON("blackList.ce", function(data) { pn = 1; var totalCount = data.totalCount; // 總記錄數(shù) var pageSize = 10; // 每頁顯示幾條記錄 var pageTotal = Math.ceil(totalCount / pageSize); // 總頁數(shù) $("#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實(shí)現(xiàn)異步分頁
最近一個(gè)項(xiàng)目要求實(shí)現(xiàn)異步分頁,簡(jiǎn)單的寫了一下,不好的請(qǐng)指出~
/** *分頁類 */ public class PageBean { publicint rowCount = 0; // 總記錄數(shù) publicint currentPage = 1;// 當(dāng)前頁數(shù) publicint sizePerPage = 20;// 每頁顯示條數(shù) publicint pageCount = 0;// 總頁數(shù) publicString pageURL;// 請(qǐng)求URL publicString pageDisplay;// JSP頁面顯示 publicString pageStyle = "numberStyle";// 分頁樣式 publicint pagePreOffset = 10;// 向前偏移量 publicint pageNextOffset = 9;// 向后偏移量 publicString pageCss;// 預(yù)留 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("<div class='"+ this.pageStyle +"'><span >"); // 數(shù)字樣式 if("numberStyle".equalsIgnoreCase(this.pageStyle)) { // 如果只有一頁,不需要分頁 if(this.getPageCount() ==1) { // pageString.append("<strong> 1</strong> "); }else { if(this.currentPage >1) {// 如果當(dāng)前頁數(shù)大于1,<< <可用 pageString.append("<a class='pagination-first' "+firstClick+" title='首頁' href='" + this.pageURL +"?currentPage=1'><<</a> "); pageString.append("<a class='pagination-prev' "+preClick+"title='上一頁' href='" + this.pageURL +"?currentPage=" + (this.currentPage -1) +"'><</a> "); }else { pageString .append("<a class='pagination-first'><<</a> "); pageString .append("<a class='pagination-prev'><</a> "); } // 定義向前偏移量 intpreOffset = this.currentPage -1 > this.pagePreOffset ?this.pagePreOffset :this.currentPage -1; // 定義向后偏移量 intnextOffset = this.getPageCount() -this.currentPage >this.pageNextOffset ?this.pageNextOffset :this.getPageCount() -this.currentPage; // 循環(huán)顯示鏈接數(shù)字,范圍是從 當(dāng)前頁減向前偏移量 到 當(dāng)前頁加向后偏移量 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) {// 當(dāng)前頁要加粗顯示 pageString .append("<strong style='color:black;border:0'>" + i +"</strong> "); }else { pageString.append("<a "+numClick+"href='"+ this.pageURL +"?currentPage=" + i + "'>" + i +"</a> "); } } // 如果當(dāng)前頁小于總頁數(shù),> >>可用 if(this.currentPage <this.getPageCount()) { pageString.append("<a class='pagination-next' "+nextClick+" title='下一頁' href='" + this.pageURL +"?currentPage=" + (this.currentPage +1) +"'>></a> "); pageString.append("<a class='pagination-last' "+lastClick+"title='尾頁' href='" + this.pageURL +"?currentPage=" + (this.getPageCount()) +"'>>></a> "); }else { pageString .append("<a class='pagination-next' >></a> "); pageString .append("<a class='pagination-last'>>></a> "); } pageString.append("<select id='pageSelect' "+onChange+">"+this.getOptions()+"</select>"); } }else if("normalStyle".equalsIgnoreCase(this.pageStyle)) { if(this.getPageCount() ==1) { pageString.append("<strong> 共1頁</strong> "); }else { if(this.currentPage >1) { pageString.append("<a class='pagination-first' "+firstClick+" title='首頁' href='" + this.pageURL +"?currentPage=1'><<</a> "); pageString.append("<a class='pagination-prev' "+preClick+"title='上一頁' href='" + this.pageURL +"?currentPage=" + (this.currentPage -1) +"'><</a> "); }else { pageString .append("<a class='pagination-first'><<</a> "); pageString .append("<a class='pagination-prev'><</a> "); } pageString.append("<span class='pageinfo'>第"+this.currentPage+"頁/"+this.pageCount+"頁</span>"); if(this.currentPage <this.getPageCount()) { pageString.append("<a class='pagination-next' "+nextClick+" title='下一頁' href='" + this.pageURL +"?currentPage=" + (this.currentPage +1) +"'>></a> "); pageString.append("<a class='pagination-last' "+lastClick+"title='尾頁' href='" + this.pageURL +"?currentPage=" + (this.getPageCount()) +"'>>></a> "); }else { pageString .append("<a class='pagination-next' >></a> "); pageString .append("<a class='pagination-last'>>></a> "); } pageString.append("<select id='pageSelect' "+onChange+">"+this.getOptions()+"</select>"); } } pageString.append("</span></div>"); 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("<option value=10>10</option><option value=20>20</option><option value=30>30</option><option value=50>50</option><option value=100>100</option>"); break; case20: sb.append("<option value=20>20</option><option value=10>10</option><option value=30>30</option><option value=50>50</option><option value=100>100</option>"); break; case30: sb.append("<option value=30>30</option><option value=10>10</option><option value=20>20</option><option value=50>50</option><option value=100>100</option>"); break; case50: sb.append("<option value=50>50</option><option value=10>10</option><option value=20>20</option><option value=30>30</option><option value=100>100</option>"); break; case100: sb.append("<option value=100>100</option><option value=10>10</option><option value=20>20</option><option value=30>30</option><option value=50>50</option>"); break; } returnsb.toString(); } } //后臺(tái)調(diào)用 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) { //回調(diào)函數(shù),后臺(tái)拼接字符串返回msg //刪除原有數(shù)據(jù),添加新數(shù)據(jù) //比如:$("#displayTable>thead").nextAll().remove();$("#displayTable").append(msg); } }); }
以上就是本文給大家介紹的使用Jquery+Ajax+Json如何實(shí)現(xiàn)分頁顯示附JAVA+JQuery實(shí)現(xiàn)異步分頁,希望對(duì)大家有所幫助。
- 用jQuery中的ajax分頁實(shí)現(xiàn)代碼
- JQuery+Ajax無刷新分頁的實(shí)例代碼
- jQuery Pagination Ajax分頁插件(分頁切換時(shí)無刷新與延遲)中文翻譯版
- jQuery DataTables插件自定義Ajax分頁實(shí)例解析
- jQuery實(shí)現(xiàn)分頁功能(含ajax請(qǐng)求、后臺(tái)數(shù)據(jù)、附完整demo)
- JS+Ajax+Jquery實(shí)現(xiàn)頁面無刷新分頁以及分組 超強(qiáng)的實(shí)現(xiàn)
- 使用PHP+JQuery+Ajax分頁的實(shí)現(xiàn)
- MVC+jQuery.Ajax異步實(shí)現(xiàn)增刪改查和分頁
- jQuery插件select2利用ajax高效查詢大數(shù)據(jù)列表(可搜索、可分頁)
- jquery+Ajax實(shí)現(xiàn)簡(jiǎn)單分頁條效果
相關(guān)文章
將xml文件作為一個(gè)小的數(shù)據(jù)庫,進(jìn)行學(xué)生的增刪改查的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄獙ml文件作為一個(gè)小的數(shù)據(jù)庫,進(jìn)行學(xué)生的增刪改查的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06ajax JSONP請(qǐng)求處理回調(diào)函數(shù)jsonpCallback區(qū)分大小寫
使用ajax進(jìn)行 JSONP跨域請(qǐng)求,因?yàn)楸徽?qǐng)求的對(duì)方的回調(diào)函數(shù)名稱是無法修改,想到設(shè)置AJAX 的JSONP參數(shù)。但是發(fā)現(xiàn)根本不起作用。最后偶然發(fā)現(xiàn) jsonpcallback是區(qū)分大小寫的2013-09-09使用HTML5中postMessage知識(shí)點(diǎn)解決Ajax中POST跨域問題
這篇文章主要介紹了使用HTML5中postMessage知識(shí)點(diǎn)解決Ajax中POST跨域問題的相關(guān)資料,需要的朋友可以參考下2015-10-10使用getJSON()異步請(qǐng)求服務(wù)器返回json格式數(shù)據(jù)的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄褂胓etJSON()異步請(qǐng)求服務(wù)器返回json格式數(shù)據(jù)的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06零基礎(chǔ)學(xué)習(xí)AJAX之制作自動(dòng)校驗(yàn)的表單
這篇文章主要介紹了零基礎(chǔ)學(xué)習(xí)AJAX之制作自動(dòng)校驗(yàn)的表單,需要的朋友可以參考下2015-01-01AJAX跨域請(qǐng)求獲取JSON數(shù)據(jù)的實(shí)現(xiàn)方法
我們都知道,由于受到瀏覽器的限制,AJAX?是不允許跨域請(qǐng)求,不過可以通過使用?JSONP?來實(shí)現(xiàn),本文介紹了?JSONP?的是怎么在?jQuery,MooTools?的,Dojo?Toolkit?中實(shí)現(xiàn)的,感興趣的朋友跟隨小編一起看看吧2023-06-06