LoginSignup
2
2

More than 5 years have passed since last update.

ページ処理のクラス

Posted at

「Goooooooooogle」みたいなページ処理をするためのクラス。
全アイテム数、1ページ当たりのアイテム数、一度に表示させるページ数をコンストラクタで渡して、contentメソッドで現在表示しているページを渡すと、minに最小ページ数、maxに最大ページ数が返ってくる。
もっとエレガントな書き方したい。

#coding:utf-8
'''
Created on 2012/09/23

@author: fumix
'''

class Pager():
    '''
    ページ処理用クラス
    '''


    def __init__(self,total,page,width):
        '''
        Constructor
        '''
        self.total = total
        self.page = page
        self.width = width

    def content(self,current):
        #最大ページ数
        max_pagecount = self.total / self.page + 1

        #最大ページ数 < 表示ページ数のとき
        if max_pagecount < self.width:
            min = 1
            max = max_pagecount

        #最大ページ数 < カレントページ + 表示ページ数の半分の時
        elif max_pagecount < current + self.width / 2 :
            min = current - self.width / 2
            max = max_pagecount

        #カレントページ - 表示ページ数の半分 < 1 のとき
        elif current - self.width / 2 < 1:
            min = 1
            max = self.width

        #それ以外
        else:
            min = current - self.width / 2
            max = current + self.width / 2
        return {'min':min,'max':max}
def _test():
    page = Pager(532, 20, 10)
    print page.content(22)

if __name__ == '__main__':
    _test()
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2