欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

分页功能 封装代码

程序员文章站 2022-04-14 15:37:35
分页功能 封装代码 [TOC] 分页代码 逻辑和样式 View 使用示列 html 页面引用 bootstrap样式 ......

分页功能 封装代码

分页代码 逻辑和样式

from django.utils.safestring import mark_safe
from django.http.request import querydict

class pagination:
    """
    需要参数
    page: 当前的页码数(必填)
    all_count: 总的数据量(可填)
    per_num :  每页显示的数据量(选填)
    max_show:  最多显示的页码数(选填)
    """

    def __init__(self, page, all_count, per_num=10, max_show=11): 
        try:
            self.page = int(page)
            if self.page <= 0:
                self.page = 1
        except exception:
            self.page = 1

        # 总的数据量
        all_count = all_count
        # 每页显示的数据量  10

        # 总的页码数
        total_num, more = divmod(all_count, per_num)
        if more:
            total_num += 1
        # 最大显示的页码数
        half_show = max_show // 2

        if total_num <= max_show:
            page_start = 1
            page_end = total_num
        else:
            if self.page - half_show <= 0:
                # 页码的起始值
                page_start = 1
                # 页码的终止值
                page_end = max_show
            elif self.page + half_show > total_num:
                page_end = total_num
                page_start = total_num - max_show + 1

            else:
                # 页码的起始值
                page_start = self.page - half_show
                # 页码的终止值
                page_end = self.page + half_show

        self.page_start = page_start
        self.page_end = page_end
        self.total_num = total_num
        self.start = (self.page - 1) * per_num
        self.end = self.page * per_num

    @property
    def page_html(self):
        li_list = []

        if self.page == 1:
            li_list.append(
                '<li class="disabled"><a aria-label="previous"> <span aria-hidden="true">&laquo;</span></a></li>')
        else:
            li_list.append(
                '<li><a href="?page={}" aria-label="previous"> <span aria-hidden="true">&laquo;</span></a></li>'.format(
                    self.page - 1))

        for i in range(self.page_start, self.page_end + 1):
            if i == self.page:
                li_list.append('<li class="active"><a href="?page={}">{}</a></li>'.format(i, i))
            else:
                li_list.append('<li><a href="?page={}">{}</a></li>'.format(i, i))

        if self.page == self.total_num:
            li_list.append(
                '<li class="disabled"><a aria-label="next"> <span aria-hidden="true">&raquo;</span></a></li>')
        else:
            li_list.append(
                '<li><a href="?page={}" aria-label="next"> <span aria-hidden="true">&raquo;</span></a></li>'.format(
                    self.page + 1))

        return ''.join(li_list)

view 使用示列

# 公私户,批量操作
class customerlist(view):
    def get(self, request, *args, **kwargs):
        q = self.search(["qq", "name", "phone"])
        if request.path_info == reverse('customer_list'):
            title = '公共客户'
            all_customer = models.customer.objects.filter(q, consultant__isnull=true)
        else:
            title = '我的客户'
            all_customer = models.customer.objects.filter(q, consultant=request.user_obj)
        page = pagination(request.get.get('page', 1), all_customer.count(), request.get.copy(), 3)
        return render(request, 'consultant/customer_list.html',
                      {"all_customer": all_customer[page.start:page.end], 'page_html': page.page_html, "title": title}) #传入参数

html 页面引用 bootstrap样式

<script src="bootstrap-3.3.7/css/bootstrap.css"></script>
<nav aria-label="page navigation">
    <ul class="pagination">
       {{ page_html }}
    </ul>
</nav>

分页功能 封装代码