trollolodn

#jquery: limit the paging buttons show + #twitter-bootstrap

May 24th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Twitter: twitter.com/quocvuongdn */
  2. /* Site: quocvuongdn.com */
  3. /* Gmail: [email protected] */
  4.  
  5. function pagination_func(pageNumbers, objHtml){
  6.     maxPage = pageNumbers; //Numbers count to show
  7.     limitPageShow = 5; //Limit page
  8.     firstPage = 0; //ID first page
  9.     lastPage = 0;
  10.     arrOnlyPager = new Array(); //Array containing the id of pages
  11.     button_pager = objHtml;
  12.    
  13.     html_pager_link = '';
  14.     if(maxPage <= limitPageShow &&  maxPage > 1){
  15.         //Chỉ đơn thuần là link
  16.         for(var i= 0; i < maxPage; i++){
  17.             html_pager_link += '<a href="#" class="btn" data-id="'+i+'">'+(i+1)+'</a>';
  18.         }
  19.     } else if(maxPage > limitPageShow){
  20.         //Thêm nút prev, next
  21.         html_pager_link = '<a href="#" class="btn" data-action="prev"><<</a>';
  22.         for(var i= 0; i < limitPageShow; i++){
  23.             html_pager_link += '<a href="#" class="btn" data-id="'+i+'">'+(i+1)+'</a>';
  24.         }
  25.         html_pager_link += '<a href="#" class="btn" data-action="next">>></a>';
  26.     }
  27.    
  28.     $(button_pager).html(html_pager_link);
  29.     $(button_pager).parent().append('<p id="cuzP" style="text-align: center;">Page 1/'+maxPage+'</p>');
  30.    
  31.     $(button_pager+' a').on('click', function(e){
  32.         e.preventDefault();
  33.         pageId = $(this).attr('data-id');
  34.         if(pageId == undefined){
  35.             pageAction = $(this).attr('data-action');
  36.             pageList = $(button_pager+' a');
  37.             if(pageAction == 'next'){
  38.                 if(firstPage+limitPageShow < maxPage){
  39.                     arrOnlyPager = new Array();
  40.                     //Update tất cả các trang đang hiển thị + thêm 1
  41.                     num_pageList = pageList.length;
  42.                     for(var i=0; i < num_pageList; i++){
  43.                         if(pageList[i].dataset.id){
  44.                             newId = parseInt(pageList[i].dataset.id)+1;
  45.                             newText = parseInt(pageList[i].innerHTML)+1;
  46.                             $(pageList[i]).attr('data-id', newId);
  47.                             $(pageList[i]).text(newText);
  48.                             arrOnlyPager.push(newId);
  49.                         }
  50.                     }
  51.                     firstPage = parseInt($(pageList[1]).attr('data-id'));
  52.                     lastPage = (firstPage+5) - 1;
  53.                 } else{
  54.                     return false;
  55.                 }
  56.             } else if(pageAction == 'prev'){
  57.                 if(firstPage > 0){
  58.                     arrOnlyPager = new Array();
  59.                     num_pageList = pageList.length;
  60.                     for(var i=0; i < num_pageList; i++){
  61.                         if(pageList[i].dataset.id){
  62.                             newId = parseInt(pageList[i].dataset.id)-1;
  63.                             newText = parseInt(pageList[i].innerHTML)-1;
  64.                             $(pageList[i]).attr('data-id', newId);
  65.                             $(pageList[i]).text(newText);
  66.                             arrOnlyPager.push(newId);
  67.                         }
  68.                     }
  69.                     firstPage = parseInt($(pageList[1]).attr('data-id'));
  70.                     lastPage = (firstPage+5) - 1;
  71.                 } else{
  72.                     return false;
  73.                 }
  74.             }
  75.         } else{
  76.             //action callback, can using eval javascript
  77.             //pageId is current page.
  78.             currentStatus = 'Page '+(parseInt(pageId)+1)+'/'+maxPage;
  79.             $(button_pager).parent().find('#cuzP').text(currentStatus);
  80.         }
  81.         //Action finally, if you want?
  82.     });
  83.  
  84. }
  85.  
  86.  
  87. //Example
  88. //data.result is maxPage
  89. // '#top_ten' is parent control, it will contains child tags
  90.  
  91. //pagination_func(data.result,'#top_ten');
  92. //demo live
  93. http://jsbin.com/egepic/1/
Advertisement
Add Comment
Please, Sign In to add comment