標籤:容量 -o 流行 isa 技術分享 網頁 math 繪製 其他
現在web注重使用者體驗與互動性,ajax 提交資料的方式很早就流行了,它能夠在不重新整理網頁的情況下局部重新整理資料。前端分頁多是用ajax請求資料(其他方式也有比如手動構造表單類比提交事件等)。通過js將查詢參數構造好發向後台,幕後處理後以特定的格式返回,多為json,比較流行處理起來也很方便。當後台資料到達後,瀏覽器重新渲染介面當然也包括js分頁控制項,如果覺得每次繪製分頁控制項對前端效能有影響也可以不繪製,但實現起來相對麻煩。
本人寫的分頁控制項參考了其他網友的代碼,連結忘了,控制項接受四個參數或一個對象,其實是一樣的對於後者只不過將四個參數放在一個對象中。pIndex:每次請求的頁碼,pSize:每次請求的頁容量,container: 放分頁控制項的容器,fn:如何向伺服器請求資料
代碼中主要用到了閉包,將上一次的請求資訊儲存起來,以備下次使用,雖然代碼可以直接拿來用但是樣式不是通用的,需要每次調樣式還好樣式比較簡單。
1 function pagination(obj){ 2 /*pageIndex: index, 3 pageSize: size, 4 count: count, 5 container: container, 6 fn : fn 7 */ 8 if(!obj||typeof obj!="object"){ 9 return false; 10 } 11 var pageIndex= obj.pageIndex||1, 12 pageSize=obj.pageSize||10, 13 count= obj.count||0, 14 container= obj.container, 15 callback=obj.fn||function(){}; 16 var pageCount =Math.ceil(count/pageSize); 17 if(pageCount==0){ 18 return false ; 19 } 20 if(pageCount<pageIndex){ 21 return false; 22 } 23 /*事件綁定*/ 24 function bindEvent(){ 25 //上一頁事件 26 $(container).find(">ul>.pg-prev").unbind("click").bind("click",function(){ 27 if(pageIndex <=1){ 28 return false ; 29 } 30 if(typeof callback=="function"){ 31 pageIndex--; 32 pageIndex = pageIndex<1?1:pageIndex; 33 obj.pageIndex= pageIndex; 34 callback(pageIndex); 35 pagination(obj); 36 } 37 }); 38 //下一頁事件 39 $(container).find(">ul>.pg-next").unbind("click").bind("click",function(){ 40 if(pageIndex ==pageCount){ 41 return false ; 42 } 43 if(typeof callback=="function"){ 44 pageIndex++; 45 pageIndex =pageIndex >pageCount?pageCount:pageIndex; 46 obj.pageIndex= pageIndex; 47 callback(pageIndex); 48 pagination(obj); 49 } 50 }); 51 $(container).find(">ul>li:not(.pg-more):not(.pg-prev):not(.pg-next)").unbind("click").bind("click",function(){ 52 pageIndex= +$(this).html(); 53 pageIndex = isNaN(pageIndex)?1:pageIndex; 54 obj.pageIndex= pageIndex; 55 if(typeof callback=="function"){ 56 callback(pageIndex); 57 pagination(obj); 58 } 59 }); 60 }; 61 62 /*畫樣式*/ 63 function printHead(){ 64 var html=[]; 65 html.push(‘<li class="pg-prev ‘+(pageIndex==1?"pg-disabled":"")+‘">上一頁</li>‘); 66 return html.join(""); 67 } 68 function printBody(){ 69 var html=[]; 70 var render=function(num,start){ 71 start=start||1; 72 for(var i=start;i<=num;i++){ 73 html.push(‘<li class="‘+(pageIndex==i?"pg-on":"")+‘">‘+i+‘</li>‘); 74 } 75 } 76 if(pageCount<=7){ 77 render(pageCount); 78 }else{ 79 if(pageIndex <4){ 80 render(4); 81 html.push(‘<li class="pg-more">...</li>‘); 82 html.push(‘<li >‘+pageCount+‘</li>‘); 83 }else{ 84 html.push(‘<li >1</li>‘); 85 html.push(‘<li class="pg-more">...</li>‘); 86 if(pageCount-pageIndex>3){ 87 render(pageIndex+1,pageIndex-1); 88 html.push(‘<li class="pg-more">...</li>‘); 89 html.push(‘<li >‘+pageCount+‘</li>‘); 90 }else{ 91 render(pageCount,pageCount-3); 92 } 93 } 94 } 95 return html.join(""); 96 } 97 function printTail(){ 98 var html=[]; 99 html.push(‘<li class="pg-next ‘+(pageIndex==pageCount?"pg-disabled":"")+‘">下一頁</li>‘);100 return html.join("");101 }102 function show(){103 container.innerHTML= ‘<ul>‘+printHead()+printBody()+printTail()+‘</ul>‘;104 }105 show();106 bindEvent();107 }View Code
js 前端分頁空間控制項