简体   繁体   English

如何生成分页号?

[英]how to generate numbers for pagination?

I made a function that returns the number displayed for pagination. 我做了一个函数,返回显示的分页号。 I want to get something like this (parentheses is the only show where the active site): 我想得到这样的内容(括号是唯一显示活动站点的位置):

if pages < 10 如果页面<10

1 2 3 4 5 6 7 8 9 10

if pages > 10 如果页面> 10

1 2 3 ... 20 [30] 40 ... 78 79 80

where [30] is active page. 其中[30]是活动页面。 If active page < 3 如果活动页面<3

1 2 [3] 4 ... 20 30 40 ... 78 79 80

etc. My code: 等。我的代码:

    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

How to do it better? 如何做得更好?

嗯,您所做的与以下内容相同:

[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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM