自定义分页器

Posted leixiaobai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义分页器相关的知识,希望对你有一定的参考价值。

自定义分页器1,效果图如下:

技术图片

代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Xiaobai Lei
import copy


class MyPagination(object):
    """自定义分页器"""

    def __init__(self, current_page_num, all_count, request, per_page_num=10, pager_show_count=9):
        """
        :param current_page_num:  当前用户选择的页数
        :param all_count:  数据库中数据的总数
        :param request:  用户提交的数据集合(主要是要拿用户请求的参数)
        :param per_page_num:  每页显示的条数
        :param pager_show_count:  最多显示的页码个数
        """

        try:
            # 如果用户输入的是非数字,则默认显示第一页
            current_page_num = int(current_page_num)
        except Exception as e:
            current_page_num = 1

        if current_page_num < 1:
            # 如果小于1页码无效,也返回第一页
            current_page_num = 1

        self.current_page_num = current_page_num
        self.all_count = all_count
        self.per_page_num = per_page_num
        self.url_params = copy.deepcopy(request.GET)

        # 计算得到总页数
        all_count_pager, tmp = divmod(all_count, per_page_num)
        if tmp:
            all_count_pager += 1
        self.all_count_pager = all_count_pager

        self.pager_show_count = pager_show_count
        self.pager_show_half_count = (self.pager_show_count - 1) // 2

    @property
    def start(self):
        """当前页数据切片的起点"""
        return (self.current_page_num - 1) * self.per_page_num

    @property
    def end(self):
        """当前页数据切片的终点"""
        return self.current_page_num * self.per_page_num

    def page_mian(self):
        """根据用户自定义显示分页条"""
        # 如果总页数小于最多显示的页码个数,就显示全部
        if self.all_count_pager <= self.pager_show_count:
            pager_start = 1
            pager_end = self.all_count_pager + 1
        else:
            # 当页面如果<=页面页码一半的时候,页面页码不发生变化
            if self.current_page_num <= self.pager_show_half_count:
                pager_start = 1
                pager_end = self.pager_show_count + 1
            else:
                # 当页码翻到最后时
                if (self.current_page_num + self.pager_show_half_count) > self.all_count_pager:
                    pager_start = self.all_count_pager - self.pager_show_count + 1
                    pager_end = self.all_count_pager + 1
                else:
                    pager_start = self.current_page_num - self.pager_show_half_count
                    pager_end = self.current_page_num + self.pager_show_half_count + 1

        # 存放分页的所有html标签
        page_html_list = []

        self.url_params[page] = 1
        first_page = <li><a href="?%s">首页</a></li> % (self.url_params.urlencode())
        page_html_list.append(first_page)

        if self.current_page_num <= 1:
            prev_page = <li class="disabled"><a href="javascript:void(0);">上一页</a></li>
        else:
            self.url_params[page] = self.current_page_num - 1
            prev_page = <li><a href="?%s">上一页</a></li> % (self.url_params.urlencode())
        page_html_list.append(prev_page)

        for i in range(pager_start, pager_end):
            self.url_params[page] = i
            if i == self.current_page_num:
                temp = <li class="active"><a href="?%s">%s</a></li> % (self.url_params.urlencode(), i)
            else:
                temp = <li><a href="?%s">%s</a></li> % (self.url_params.urlencode(), i)
            page_html_list.append(temp)

        if self.current_page_num >= self.all_count_pager:
            next_page = <li class="disabled"><a href="javascript:void(0);">下一页</a></li>
        else:
            self.url_params[page] = self.current_page_num + 1
            next_page = <li><a href="?%s">下一页</a></li> % (self.url_params.urlencode())
        page_html_list.append(next_page)

        self.url_params[page] = self.all_count_pager
        last_page = <li><a href="?%s">尾页</a></li> % (self.url_params.urlencode())
        page_html_list.append(last_page)

        return ‘‘.join(page_html_list)

 

以上是关于自定义分页器的主要内容,如果未能解决你的问题,请参考以下文章

swiper自定义分页器

Django分页器及自定义分页器

自定义分页器

Django分页器和自定义分页器

Django Ajax与自定义分页器

自定义分页器