zoukankan      html  css  js  c++  java
  • 分页函数 pager.py

    #!/usr/bin/python env
    # coding:utf-8
    
    class Pagination(object):
                      # 数据总条数     当前页        每页显示条数     最多显示页面
        def __init__(self, totalCount, currentPage, perPageItemNum=10, maxPageNum=7):
            # 数据总个数
            self.total_count = totalCount
            # 当前页
            try:
                v = int(currentPage)
                if v <= 0:
                    v = 1
                self.current_page = v
            except Exception as e:
                self.current_page = 1
            # 每页显示的条数
            self.per_page_item_num = perPageItemNum
            # 最多显示页面数
            self.max_page_num = maxPageNum
    
        # 开始索引
        @property
        def start(self):
            return (self.current_page - 1) * self.per_page_item_num
    
        # 结束索引
        @property
        def end(self):
            return self.current_page * self.per_page_item_num
    
        # 总页数
        @property
        def num_pages(self):
            a, b = divmod(self.total_count, self.per_page_item_num)
            if b == 0:
                return a
            return a + 1
    
        def pager_num_range(self):
            if self.num_pages < self.max_page_num:
                return range(1, self.num_pages + 1)
            # 总页数特别多 5
            part = int(self.max_page_num / 2)
            if self.current_page <= part:
                return range(1, self.max_page_num + 1)
            if (self.current_page + part) > self.num_pages:
                return range(self.num_pages - self.max_page_num + 1, self.num_pages + 1)
            return range(self.current_page - part, self.current_page + part + 1)
    
        def page_str(self):
            page_list = []
    
            first = "<li><a href='/index2.html?p=1'>首页</a></li>"
            page_list.append(first)
    
            if self.current_page == 1:
                prev = "<li><a href='#'>上一页</a></li>"
            else:
                prev = "<li><a href='/index2.html?p=%s'>上一页</a></li>" % (self.current_page - 1,)
            page_list.append(prev)
            for i in self.pager_num_range():
                if i == self.current_page:
                    temp = "<li class='active'><a href='/index2.html?p=%s'>%s</a></li>" % (i, i)
                else:
                    temp = "<li><a href='/index2.html?p=%s'>%s</a></li>" % (i, i)
                page_list.append(temp)
    
            if self.current_page == self.num_pages:
                nex = "<li><a href='#'>下一页</a></li>"
            else:
                nex = "<li><a href='/index2.html?p=%s'>下一页</a></li>" % (self.current_page + 1,)
            page_list.append(nex)
    
            last = "<li><a href='/index2.html?p=%s'>尾页</a></li>" % (self.num_pages,)
            page_list.append(last)
    
            return ''.join(page_list)
  • 相关阅读:
    QML学习笔记(三)-引入Font-awesome
    QML学习笔记(一)-防止鼠标穿透事件
    JS小积累(一)— 判断在线离线
    electron入门笔记(三)- 引入bootstrap
    express搭建服务器
    生成SSH密钥添加到GitHub
    python中常见的错误
    PyCharm在同一个包(package)下,如何把一个.py文件导入另外一个.py文件下
    在PyCharm中导入Numpy和Pygame模块 (win8.1)
    Pycharm中安装Pygame并写第一个程序
  • 原文地址:https://www.cnblogs.com/icemonkey/p/10536421.html
Copyright © 2011-2022 走看看