zoukankan      html  css  js  c++  java
  • stark组件之分页【模仿Django的admin】

    我们的stark组件用的我们的分页组件,没有重新写

    下面直接看下分页的代码

    class page_helper():
        def __init__(self, count, current_page, per_page_num, base_url,parms):
            self.count = count
            self.current_page = current_page
            self.per_page_num = per_page_num
            self.base_url = base_url
            self.page_head = "<nav aria-label='Page navigation'><ul class='pagination'>"
            self.page_tail = "</ul></nav>"
            import copy
            parms = copy.deepcopy(parms)
            parms._mutable = True
            self.parms = parms
    
    
        def page_count(self):
            before, after = divmod(self.count, self.per_page_num)
            if after > 0:
                class_count = before + 1
            else:
                class_count = before
            print("-------------",class_count,self.count,self.per_page_num)
            return class_count
    
        def page_start(self):
            if self.page_count() <= 11:
                start_int = 1
                end_int = self.page_count()
            else:
                if self.current_page <= 6:
                    start_int = 1
                    end_int = 11
                elif self.current_page + 5 + 1 > self.page_count():
                    start_int = self.page_count() - 10
                    end_int = self.page_count() + 1
                else:
                    start_int = self.current_page - 5
                    end_int = self.current_page + 5 + 1
            return start_int
    
        def page_end(self):
            if self.page_count() <= 11:
                start_int = 1
                end_int = self.page_count()
            else:
                if self.current_page <= 6:
                    start_int = 1
                    end_int = 11
                elif self.current_page + 5 + 1 > self.page_count():
                    start_int = self.page_count() - 10
                    end_int = self.page_count() + 1
                else:
                    start_int = self.current_page - 5
                    end_int = self.current_page + 5 + 1
    
            return end_int
    
        def db_start(self):
            return (self.current_page - 1) * self.per_page_num + 1
    
        def db_end(self):
            return self.current_page * self.per_page_num + 1
    
        def page_list(self):
            print(self.parms,dir(self.parms),"======================",self.parms.urlencode())
            if self.current_page == 1:
                before_page = "<li class='disable'><a href='#' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>"
            else:
                self.parms["p"] = self.current_page - 1
                before_page = "<li class='disable'><a href='{url}?{args}' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>".format(url=self.base_url, args=self.parms.urlencode())
            page_list = []
            page_list.append(before_page)
    
            for i in range(self.page_start(), self.page_end() + 1):
                self.parms["p"] = i
                if i == self.current_page:
    
                    s = """<li class="active"><a href="{url}?{args}">{num}</a></li>""".format(url=self.base_url, args=self.parms.urlencode(),num=self.parms["p"])
                else:
                    s = """<li><a href="{url}?{args}">{num}</a></li>""".format(url=self.base_url,  args=self.parms.urlencode(),num=self.parms["p"])
                page_list.append(s)
            if self.current_page == self.page_count():
                after_page = """<li class='disable'><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>"""
            else:
                self.parms["p"] = self.current_page + 1
                after_page = """<li><a href="{url}?{args}" aria-label="Next"><span aria-hidden="true">»</span></a></li>""".format(url=self.base_url, args=self.parms.urlencode())
            page_list.append(after_page)
            page_body = "".join(page_list)
            page_str = self.page_head + page_body + self.page_tail
    
            return page_str
    

      

    分页的代码在什么实用用呢?应该list_url这个函数把过滤和搜索后的数据传递个showlist这个类,然后这个类在根据这些要显示的数据在进行分页

    class Showlist(object):
        def __init__(self,config,request,new_list,new_data_list):
            self.config = config
            self.request = request
            self.new_list = new_list
            self.new_data_list = new_data_list
    
            # 分页显示
            count = self.new_data_list.count()
            current_page = int(request.GET.get("p",1))
            per_page_num = 3
            base_url = request.path_info
            parms = request.GET
            self.page_str = page.page_helper(count=count,current_page=current_page,per_page_num=per_page_num,base_url=base_url,parms=parms)
    

      

  • 相关阅读:
    【C++】资源管理
    【Shell脚本】逐行处理文本文件
    【算法题】rand5()产生rand7()
    【Shell脚本】字符串处理
    Apple iOS产品硬件参数. 不及格的程序员
    与iPhone的差距! 不及格的程序员
    iPhone游戏 Mr.Karoshi"过劳死"通关. 不及格的程序员
    XCode V4 发布了, 苹果的却是个变态. 不及格的程序员
    何时readonly 字段不是 readonly 的?结果出呼你想象!!! 不及格的程序员
    object file format unrecognized, invalid, or unsuitable Command 不及格的程序员
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/10134955.html
Copyright © 2011-2022 走看看