[英]how to generate numbers for pagination?
我做了一個函數,返回顯示的分頁號。 我想得到這樣的內容(括號是唯一顯示活動站點的位置):
如果頁面<10
1 2 3 4 5 6 7 8 9 10
如果頁面> 10
1 2 3 ... 20 [30] 40 ... 78 79 80
其中[30]是活動頁面。 如果活動頁面<3
1 2 [3] 4 ... 20 30 40 ... 78 79 80
等。我的代碼:
count_all - number of all items
items_on_page - items displayed on one page
page - current page
countpages = int(float(count_all+(items_on_page-1))/float(items_on_page))
linkitems = 10
if countpages > (linkitems-1):
countpagesstr = list()
for i in range(1,int(float(linkitems+(2))/float(3))):
countpagesstr += [str(i)]
countpagesstr += ['...']
sr = int(countpages/2)
for i in range(sr-1, sr+1):
countpagesstr += [str(i)]
countpagesstr += ['...']
for i in range(countpages-3, countpages):
countpagesstr += [str(i)]
else:
cp = list()
for c in range(1,countpages+1):
cp.append(str(c))
countpagesstr = cp
return countpagesstr
如何做得更好?
嗯,您所做的與以下內容相同:
[str(i) for i in range((linkitems + 2) / 3)] + [' ... ', str(active_page - 1), '[' + str(active_page) + ']', str(active_page + 1), '...'] + [str(i) for i in range(cntitems-3, cntitems)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.