一、自定义分页遵守的
-
当前页,当前页可能会被传入特别的值建议异常就是第一页
-
底栏展示页码
-
数据总个数用来计算生成多少页
-
每页展示页数
逻辑
-
首先我们利用切片,把数据分成段落显示,每一页的第一条数据是(当前页-1)*页面显示条数
@property
def start(self):
'''
:return: 开始页数
'''
return (self.current_page-1)*self.page_count
@property
def end(self):
'''
:return: 结束页数
'''
return (self.current_page*self.page_count)
@property
def total_count(self):
'''
:return: 总页数
'''
v, y = divmod(self.data_count,self.page_count)
if y:
v +=1
return v
-
页面展示的条数也就是循环次数
- 当总页数小于页面实际展示页数时,开始页1,结束页实际页数
- 当总页数大于页面实际展示页数时
- 第一种情况当前页小于中间页,开始页1,结束页是页面展示底栏
- 第二种情况当前页大于中间页,开始页当前页-(中间页-1)/2,结束页(当前页+1)/2
- 当然第二种的前提建立在(当前页+(中间页-1)/2)大于总页数
开始页总页数-底栏数-1,结束页等于总页数+1
_author__ = 'Administrator'
from django.utils.safestring import mark_safe
class Pagination(object):
def __init__(self, current_page, data_count, per_page_count=1, pager_num=7):
try:
self.current_page = int(current_page)
except Exception as e:
self.current_page = 1
self.data_count = data_count
self.per_page_count = per_page_count
self.pager_num = pager_num
@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
@property
def total_count(self):
v, y = divmod(self.data_count, self.per_page_count)
if y:
v += 1
return v
def page_str(self, base_url):
page_list = []
if self.total_count < self.pager_num:
start_index = 1
end_index = self.total_count + 1
else:
if self.current_page <= (self.pager_num + 1) / 2:
start_index = 1
end_index = self.pager_num + 1
else:
start_index = self.current_page - (self.pager_num - 1) / 2
end_index = self.current_page + (self.pager_num + 1) / 2
if (self.current_page + (self.pager_num - 1) / 2) > self.total_count:
end_index = self.total_count + 1
start_index = self.total_count - self.pager_num + 1
if self.current_page == 1:
prev = '<li><a class="page" href="javascript:void(0);">上一页</a></li>'
else:
prev = '<li><a class="page" href="%s?p=%s">上一页</a></li>' % (base_url, self.current_page - 1,)
page_list.append(prev)
for i in range(int(start_index), int(end_index)):
if i == self.current_page:
temp = '<li class="active"><a class="page active" href="%s?p=%s">%s</a></li>' % (base_url, i, i)
else:
temp = '<li><a class="page" href="%s?p=%s">%s</a></li>' % (base_url, i, i)
page_list.append(temp)
if self.current_page == self.total_count:
nex = '<li><a class="page" href="javascript:void(0);">下一页</a></li>'
else:
nex = '<li><a class="page" href="%s?p=%s">下一页</a></li>' % (base_url, self.current_page + 1,)
page_list.append(nex)
# jump = """
# <input type='text' /><a onclick='jumpTo(this, "%s?p=");'>GO</a>
# <script>
# function jumpTo(ths,base){
# var val = ths.previousSibling.value;
# location.href = base + val;
# }
# </script>
# """ % (base_url,)
#
# page_list.append(jump)
page_str = mark_safe("".join(page_list))
return page_str