Pagination

499 views
Skip to first unread message

Web2Py Freak

unread,
Aug 6, 2011, 4:00:35 AM8/6/11
to web2py-users
Hey guys ,

Anyone Used Pagination before , i have a table named "blogpost" and i
want it to view every 10 post in a page , how can i do that ??

pbreit

unread,
Aug 6, 2011, 5:17:46 AM8/6/11
to web...@googlegroups.com
Probably have to code it. There's an example in the book.

Bruno Rocha

unread,
Aug 6, 2011, 5:18:45 AM8/6/11
to web...@googlegroups.com

db(db.posts.id>0).select(limitby=(0,10))

http://zerp.ly/rochacbruno

Web2Py Freak

unread,
Aug 6, 2011, 5:47:45 AM8/6/11
to web...@googlegroups.com
This is the example in the book  , how can i edit it to fit my table "db.blogpost"  ?

db = DAL('sqlite://primes.db')
db.define_table('prime',Field('value','integer'))
def isprime(p):
for i in range(2,p):
if p%i==0: return False
return True
if
len(db().select(db.prime.id))==0:
p=2
for i in range(1000):
while not isprime(p): p+=1
db.prime.insert(value=p)
p+=1



def list_items():
if len(request.args): page=int(request.args[0])
else: page=0
items_per_page=20
limitby=(page*items_per_page,(page+1)*items_per_page+1)
rows=db().select(db.prime.ALL,limitby=limitby)
return dict(rows=rows,page=page,items_per_page=items_per_page)

Notice that this code selects one more item than is needed, 20+1. The extra element tells the view whether there is a next page.

Here is the "default/list_items.html" view:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
{{extend 'layout.html'}}

{{for i,row in enumerate(rows):}}
{{if i==items_per_page: break}}
{{=row.value}}<br />
{{pass}}

{{if page:}}
<a href="{{=URL(args=[page-1])}}">previous</a>
{{pass}}

{{if len(rows)>items_per_page:}}
<a href="{{=URL(args=[page+1])}}">next</a>
{{pass}}

Massimo Di Pierro

unread,
Aug 6, 2011, 10:58:35 AM8/6/11
to web2py-users
You can do something like this:

class Paginator(DIV):
items_per_page = 10
records = 100
def limitby(self):
from gluon import current
page = self.page=int(current.request.vars.page or 0)
return (self.items_per_page*page,self.items_per_page*(page
+1))
def xml(self):
from gluon import current
pages,rem = divmod(self.records,self.items_per_page)
if rem: pages+=1
if self.page>0:

self.append(A('first',_href=URL(args=current.request.args,vars=dict(page=0))))
if self.page>1:

self.append(A('prev',_href=URL(args=current.request.args,vars=dict(page=self.page-1))))
if self.page<pages-2:

self.append(A('next',_href=URL(args=current.request.args,vars=dict(page=self.page
+1))))
if self.page<pages-1:

self.append(A('last',_href=URL(args=current.request.args,vars=dict(page=pages-1))))
return DIV.xml(self)

def index():
p=Paginator()
p.items_per_page=5
p.records=db(query).count()
rows = db(query).select(limitby=p.limitby())
return dict(rows=rows, paginator=p)

{{extend 'layout.html'}}
{{=SQLTABLE(rows)}}
{{=paginator}}

customize the xml function above to make it look better!

pbreit

unread,
Aug 6, 2011, 12:36:31 PM8/6/11
to web...@googlegroups.com
Where does the class go? In a module?

I still don't fully understand "current". Is that an easy way for a module to have access to the whole current environment?

Massimo Di Pierro

unread,
Aug 6, 2011, 1:10:36 PM8/6/11
to web2py-users
You can put anywhere you like. If it goes in a module, you need to
import it.

Because of the new custom import and current. This is no longer a
problem.

Any module imported by a model or controller can do from gluon import
current, DIV, SQLHTML, etc. and get access to everything. current
contains current.request, current.response, current.session,
current.T, current.cache + anything your models want to pass want to
pass to the modules.
Reply all
Reply to author
Forward
0 new messages