js 前端分頁空間控制項

來源:互聯網
上載者:User

標籤:容量   -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 前端分頁空間控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.