Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
paginator
#!/usr/bin/env python
#coding=utf-8
from __future__ import division
import math
class Paginator():
"""系统查询分页工具
"""
def __init__(self, url_func, page=1, total=0, page_size=20):
self.url_func = url_func
self.page = 1 if int(page) < 1 else int(page)
self.total = int(total)
self.page_size = int(page_size)
self.page_num = int(math.ceil(self.total / self.page_size)) if self.total > 0 else 0
self.page_bars = {}
self.data = ()
for _page in range(1, self.page_num + 1):
_index = int(_page / 10)
if not self.page_bars.has_key(_index):
self.page_bars[_index] = {_page}
else:
self.page_bars[_index].add(_page)
def render(self,form_id=None):
'''
动态输出html内容
'''
page_bar = self.page_bars.get(int(self.page / 10))
if page_bar is None:
return ''
_htmls = []
if form_id:
_htmls.append('''<script>
function goto_page(form_id,page){
var form=document.getElementById(form_id);
var page_input = document.createElement("input");
page_input.type="hidden";
page_input.name="page";
page_input.value=page;
form.appendChild(page_input);
form.submit();
}</script>''')
_htmls.append('<ul class="pagination pull-right">')
_htmls.append('\t<li class="disabled"><a href="#">查询记录数 %s</a></li>' % self.total)
current_start = self.page
if current_start == 1:
_htmls.append('\t<li class="disabled"><a href="#">首页</a></li>')
_htmls.append('\t<li class="disabled"><a href="#">&larr; 上一页</a></li>')
else:
_htmls.append('\t<li><a href="%s">首页</a></li>' % self.url_func(1,form_id))
_htmls.append('\t<li><a href="%s">&larr; 上一页</a></li>' % self.url_func(current_start - 1,form_id))
for page in page_bar:
_page_url = self.url_func(page,form_id)
if page == self.page:
_htmls.append('\t<li class="active"><span>%s <span class="sr-only">{current}</span></span></li>' % page)
else:
_htmls.append('\t<li><a href="%s">%s</a></li>' % (_page_url, page))
current_end = self.page
if current_end == self.page_num:
_htmls.append('\t<li class="disabled"><a href="#">下一页 &rarr;</a></li>')
_htmls.append('\t<li class="disabled"><a href="#">尾页</a></li>')
else:
_htmls.append('\t<li><a href="%s">下一页 &rarr;</a></li>' % self.url_func(current_end + 1,form_id))
_htmls.append('\t<li><a href="%s">尾页</a></li>' % self.url_func(self.page_num,form_id))
_htmls.append('</ul>')
return '\r\n'.join(_htmls)
@jamiesun

This comment has been minimized.

Copy link
Owner Author

commented Jun 10, 2014

页面 handler 中的使用

query = self.db.query(MpsUser).filter(MpsUser.subscribe==1)
page_data = self.get_page_data(query)

def get_page_data(self, query):
    page_size = self.application.settings.get("page_size",20)

  page = int(self.get_argument("page", 1))
    offset = (page - 1) * page_size
      result = query.limit(page_size).offset(offset)
       page_data = Paginator(self.get_page_url, page, query.count(), page_size)
    page_data.result = result
      return page_data


def get_page_url(self, page,form_id=None):
    if form_id:
        return "javascript:goto_page('%s',%s);" %(form_id.strip(),page)
    path = self.request.path
    query = self.request.query
    qdict = urlparse.parse_qs(query)
    for k, v in qdict.items():
        if isinstance(v, list):
            qdict[k] = v and v[0] or ''
    qdict['page'] = page
    return path + '?' + urllib.urlencode(edict)

mako模板

% for user in page_data.result:

%endfor

${page_data.render(form_id="query_form")}

如果有查询表单,把表单 id 传进去

@qloog

This comment has been minimized.

Copy link

commented Jun 10, 2014

赞~很实用,stat it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.