19 ##### 屬性方法案例-資料分頁

jhchena發表於2024-09-27
class Pagination:

    def __init__(self, total_count, per_page_count, page_num):
        """

        :param total_count:  資料總條數
        :param per_page_count: 每頁資料顯示的條數
        :param page_num: 當前檢視的頁數
        """
        self.total_count = total_count
        self.per_page_count = per_page_count
        self.page_num = page_num

    # 每頁資料顯示10條資料
    # 第1頁索引: 0:10
    # 第2頁索引: 11:20
    # 第3頁索引: 21:30

    @property
    def start(self):
        return (self.page_num - 1) * self.per_page_count

    @property
    def end(self):
        return self.page_num * self.per_page_count


data_list = [1, 2, 34, 4, 5, 56, 11, 1, 243, 1234]

pager = Pagination(192, 10, 3)

ret = data_list[pager.start:pager.end]  # 屬性方法呼叫時,就不需要加括號了
print(ret)

相關文章