内容简介:分页组件
class Pagination(object): """ 自定义分页组件的使用方法: pager_obj = Pagination(request.GET.get('page',1),len(HOST_LIST),request.path_info,request.GET) host_list = HOST_LIST[pager_obj.start:pager_obj.end] html = pager_obj.page_html() return render(request,'hosts.html',{'host_list':host_list,"page_html":html}) """ def __init__(self,current_page,total_count,base_url,params,per_page_count=10,max_pager_count=11): try: current_page = int(current_page) except Exception as e: current_page = 1 if current_page <=0: current_page = 1 self.current_page = current_page # 数据总条数 self.total_count = total_count # 每页显示10条数据 self.per_page_count = per_page_count # 页面上应该显示的最大页码 max_page_num, div = divmod(total_count, per_page_count) if div: max_page_num += 1 self.max_page_num = max_page_num # 页面上默认显示11个页码(当前页在中间) self.max_pager_count = max_pager_count self.half_max_pager_count = int((max_pager_count - 1) / 2) # URL前缀 self.base_url = base_url # request.GET import copy params = copy.deepcopy(params) params._mutable = True # 包含当前列表页面所有的搜索条件 # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} # self.params[page] = 8 # self.params.urlencode() # source=2&status=2&gender=2&consultant=1&page=8 # href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8" # href="%s?%s" %(self.base_url,self.params.urlencode()) self.params = params @property def start(self): return (self.current_page - 1) * self.per_page_count @property def end(self): return self.current_page * self.per_page_count def page_html(self): # 如果总页数 <= 11 if self.max_page_num <= self.max_pager_count: pager_start = 1 pager_end = self.max_page_num # 如果总页数 > 11 else: # 如果当前页 <= 5 if self.current_page <= self.half_max_pager_count: pager_start = 1 pager_end = self.max_pager_count else: # 当前页 + 5 > 总页码 if (self.current_page + self.half_max_pager_count) > self.max_page_num: pager_end = self.max_page_num pager_start = self.max_page_num - self.max_pager_count + 1 #倒这数11个 else: pager_start = self.current_page - self.half_max_pager_count pager_end = self.current_page + self.half_max_pager_count page_html_list = [] # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} # 首页 self.params['page'] = 1 first_page = '<li><a href="%s?%s">首页</a></li>' % (self.base_url,self.params.urlencode(),) page_html_list.append(first_page) # 上一页 if self.current_page != 1: self.params['page']=self.current_page-1 l='<li><a href="%s?%s">上一页</a></li>'% (self.base_url,self.params.urlencode(),) page_html_list.append(l) for i in range(pager_start, pager_end + 1): self.params['page'] = i if i == self.current_page: temp = '<li class="active"><a href="%s?%s">%s</a></li>' % (self.base_url,self.params.urlencode(), i,) else: temp = '<li><a href="%s?%s">%s</a></li>' % (self.base_url,self.params.urlencode(), i,) page_html_list.append(temp) # 下一页 if self.current_page < self.max_page_num: self.params['page']=self.current_page+1 k='<li><a href="%s?%s">下一页</a></li>'% (self.base_url,self.params.urlencode(),) page_html_list.append(k) self.params['page'] = self.max_page_num # 尾页 self.params['page'] = self.max_page_num last_page = '<li><a href="%s?%s">尾页</a></li>' % (self.base_url, self.params.urlencode(),) page_html_list.append(last_page) return ''.join(page_html_list)
class Pagination(object): """ 自定义分页 """ def __init__(self,current_page,total_count,base_url,params,per_page_count=10,max_pager_count=11): try: current_page = int(current_page) except Exception as e: current_page = 1 if current_page <=0: current_page = 1 self.current_page = current_page # 数据总条数 self.total_count = total_count
# 每页显示10条数据 self.per_page_count = per_page_count
# 页面上应该显示的最大页码 max_page_num, div = divmod(total_count, per_page_count) if div: max_page_num += 1 self.max_page_num = max_page_num
# 页面上默认显示11个页码(当前页在中间) self.max_pager_count = max_pager_count self.half_max_pager_count = int((max_pager_count - 1) / 2)
# URL前缀 self.base_url = base_url
# request.GET import copy params = copy.deepcopy(params) params._mutable = True # 包含当前列表页面所有的搜索条件 # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} # self.params[page] = 8 # self.params.urlencode() # source=2&status=2&gender=2&consultant=1&page=8 # href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8" # href="%s?%s" %(self.base_url,self.params.urlencode()) self.params = params
@property def start(self): return (self.current_page - 1) * self.per_page_count
@property def end(self): return self.current_page * self.per_page_count
def page_html(self): # 如果总页数 <= 11 if self.max_page_num <= self.max_pager_count: pager_start = 1 pager_end = self.max_page_num # 如果总页数 > 11 else: # 如果当前页 <= 5 if self.current_page <= self.half_max_pager_count: pager_start = 1 pager_end = self.max_pager_count else: # 当前页 + 5 > 总页码 if (self.current_page + self.half_max_pager_count) > self.max_page_num: pager_end = self.max_page_num pager_start = self.max_page_num - self.max_pager_count + 1 #倒这数11个 else: pager_start = self.current_page - self.half_max_pager_count pager_end = self.current_page + self.half_max_pager_count
page_html_list = [] # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} # 首页 self.params['page'] = 1 first_page = '<li><a href="%s?%s">首页</a></li>' % (self.base_url,self.params.urlencode(),) page_html_list.append(first_page)
# 上一页 self.params["page"] = self.current_page - 1 if self.params["page"] < 1: pervious_page = '<li class="disabled"><a href="%s?%s" aria-label="Previous">上一页</span></a></li>' % ( self.base_url, self.params.urlencode()) else: pervious_page = '<li><a href = "%s?%s" aria-label = "Previous" >上一页</span></a></li>' % ( self.base_url, self.params.urlencode()) page_html_list.append(pervious_page)
# 中间页码 for i in range(pager_start, pager_end + 1): self.params['page'] = i if i == self.current_page: temp = '<li class="active"><a href="%s?%s">%s</a></li>' % (self.base_url,self.params.urlencode(), i,) else: temp = '<li><a href="%s?%s">%s</a></li>' % (self.base_url,self.params.urlencode(), i,) page_html_list.append(temp)
# 下一页 self.params["page"] = self.current_page + 1 if self.params["page"] > self.max_page_num: self.params["page"] = self.current_page next_page = '<li class="disabled"><a href = "%s?%s" aria-label = "Next">下一页</span></a></li >' % ( self.base_url, self.params.urlencode()) else: next_page = '<li><a href = "%s?%s" aria-label = "Next">下一页</span></a></li>' % ( self.base_url, self.params.urlencode()) page_html_list.append(next_page)
# 尾页 self.params['page'] = self.max_page_num last_page = '<li><a href="%s?%s">尾页</a></li>' % (self.base_url, self.params.urlencode(),) page_html_list.append(last_page)
return ''.join(page_html_list)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- React 组件模式-有状态组件 x 无状态组件、容器组件 x 展示组件、高阶组件 x 渲染回调(函数作为子组件)
- Serverless 组件开发尝试:全局变量组件和单独部署组件
- angular自定义组件-UI组件篇-switch组件
- React Hooks 源码解析(一):类组件、函数组件、纯组件
- Vue动态组件和异步组件
- Vue 动态组件 & 异步组件原理
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
perl进阶
Randal L.Schwartz、brian d.foy、Tom Phoenix / 韩雷 / 人民邮电出版社 / 2015-10-1 / 69
本书是Learning Perl一书的进阶。学完本书之后,您可以使用Perl语言的特性编写从简单脚本到大型程序在内的所有程序,正是Perl语言的这些特性使其成为通用的编程语言。本书为读者深入介绍了模块、复杂的数据结构以及面向对象编程等知识。 本书每章的篇幅都短小精悍,读者可以在一到两个小时内读完,每章末尾的练习有助于您巩固在本章所学的知识。如果您已掌握了Learning Perl中的内容并渴......一起来看看 《perl进阶》 这本书的介绍吧!