1. 程式人生 > >分頁組件

分頁組件

首頁 bsp += 前綴 clas margin oct param max

  1 """
  2 自定義分頁組件的使用方法:
  3     pager_obj = Pagination(request.GET.get(‘page‘,1),len(HOST_LIST),request.path_info,request.GET)
  4     host_list = HOST_LIST[pager_obj.start:pager_obj.end]
  5     html = pager_obj.page_html()
  6     return render(request,‘hosts.html‘,{‘host_list‘:host_list,"page_html":html})
7 """ 8 """ 9 10 <!DOCTYPE html> 11 <html lang="en"> 12 <head> 13 <meta charset="UTF-8"> 14 <title>Title</title> 15 <style> 16 .pager a{ 17 display: inline-block; 18 padding: 2px 8px; 19 margin: 0 5px;
20 border: 1px solid cadetblue; 21 } 22 .pager a.active{ 23 background-color: #2b669a; 24 color: white; 25 } 26 </style> 27 </head> 28 <body> 29 <h1>用戶列表</h1> 30 <ul> 31 {% for item in user_list %}
32 <li>{{ item }}</li> 33 {% endfor %} 34 </ul> 35 <div class="pager"> 36 {{ page_html|safe }} 37 </div> 38 </body> 39 </html> 40 """ 41 42 class Pagination(object): 43 """ 44 自定義分頁 45 """ 46 def __init__(self,current_page,total_count,base_url,params,per_page_count=10,max_pager_count=11): 47 try: 48 current_page = int(current_page) 49 except Exception as e: 50 current_page = 1 51 if current_page <=0: 52 current_page = 1 53 self.current_page = current_page 54 # 數據總條數 55 self.total_count = total_count 56 57 # 每頁顯示10條數據 58 self.per_page_count = per_page_count 59 60 # 頁面上應該顯示的最大頁碼 61 max_page_num, div = divmod(total_count, per_page_count) 62 if div: 63 max_page_num += 1 64 self.max_page_num = max_page_num 65 66 # 頁面上默認顯示11個頁面(當前頁在中間) 67 self.max_pager_count = max_pager_count 68 self.half_max_pager_count = int((max_pager_count - 1) / 2) 69 70 # URL前綴 71 self.base_url = base_url 72 73 # request.GET 74 import copy 75 params = copy.deepcopy(params) 76 params._mutable = True 77 # 包含當前列表頁面所有的搜索條件 78 # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} 79 # self.params[page] = 8 80 # self.params.urlencode() 81 # source=2&status=2&gender=2&consultant=1&page=8 82 # href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8" 83 # href="%s?%s" %(self.base_url,self.params.urlencode()) 84 self.params = params 85 86 @property 87 def start(self): 88 return (self.current_page - 1) * self.per_page_count 89 90 @property 91 def end(self): 92 return self.current_page * self.per_page_count 93 94 def page_html(self): 95 # 如果總頁數 <= 11 96 if self.max_page_num <= self.max_pager_count: 97 pager_start = 1 98 pager_end = self.max_page_num 99 # 如果總頁數 > 11 100 else: 101 # 如果當前頁 <= 5 102 if self.current_page <= self.half_max_pager_count: 103 pager_start = 1 104 pager_end = self.max_pager_count 105 else: 106 # 當前頁 + 5 > 總頁碼 107 if (self.current_page + self.half_max_pager_count) > self.max_page_num: 108 pager_end = self.max_page_num 109 pager_start = self.max_page_num - self.max_pager_count + 1 110 else: 111 pager_start = self.current_page - self.half_max_pager_count 112 pager_end = self.current_page + self.half_max_pager_count 113 114 page_html_list = [] 115 # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} 116 self.params[page] = 1 117 first_page = <a href="%s?%s">首頁</a> % (self.base_url,self.params.urlencode(),) 118 page_html_list.append(first_page) 119 # 上一頁 120 121 for i in range(pager_start, pager_end + 1): 122 self.params[page] = i 123 if i == self.current_page: 124 temp = <a class="active" href="%s?%s">%s</a> % (self.base_url,self.params.urlencode(), i,) 125 else: 126 temp = <a href="%s?%s">%s</a> % (self.base_url,self.params.urlencode(), i,) 127 page_html_list.append(temp) 128 129 # 下一頁 130 131 self.params[page] = self.max_page_num 132 last_page = <a href="%s?%s">尾頁</a> % (self.base_url, self.params.urlencode(),) 133 page_html_list.append(last_page) 134 135 return ‘‘.join(page_html_list)

分頁組件