自定義分頁(類封裝高階版 函式低階版)

2022-05-15 15:05:00 字數 4723 閱讀 8842

類封裝高階版:

1

#自定義分頁:分頁樣式採用bootstrap

2class

pagenation:

3def

__init__(self,base_url,current_page_num,total_counts,request,per_page_counts=10,page_number=5):

4'''

5:param base_url: 分頁展示資訊的基礎路徑

6:param current_page_num: 當前頁頁碼

7:param total_counts: 總的資料量

8:param per_page_counts: 每頁展示的資料量

9:param page_number: 顯示頁碼數

10'''

11 self.base_url =base_url

12 self.current_page_num =current_page_num

13 self.total_counts =total_counts

14 self.per_page_counts =per_page_counts

15 self.page_number =page_number

16 self.request=request

17import

copy

18 self.params =copy.deepcopy(self.request.get)

19try

:20 self.current_page_num =int(self.current_page_num)

21except

exception:

22 self.current_page_num = 1

23 half_page_range = self.page_number // 224#

計算總頁數25#

self.page_number_count, a = divmod(self.total_counts, self.per_page_counts)26#

if self.current_page_num < 1:27#

self.current_page_num = 128#

if a:29#

self.page_number_count += 1

30import

math

31 self.page_number_count=math.ceil(self.total_counts/self.per_page_counts)

32if self.current_page_num < 1:

33 self.current_page_num = 1

34if self.current_page_num >self.page_number_count:

35 self.current_page_num =self.page_number_count

36if self.page_number_count <=self.page_number:

37 self.page_start = 1

38 self.page_end =self.page_number_count

39else:40

if self.current_page_num <=half_page_range:

41 self.page_start = 1

42 self.page_end =page_number

43elif self.current_page_num + half_page_range >=self.page_number_count:

44 self.page_start = self.page_number_count - self.page_number + 1

45 self.page_end =self.page_number_count

46else

:47 self.page_start = self.current_page_num -half_page_range

48 self.page_end = self.current_page_num +half_page_range49#

資料切片依據,起始位置

50@property

51def

start_num(self):

52if self.current_page_num==0:

53 self.current_page_num=1

54 start_num = (self.current_page_num - 1) *self.per_page_counts

55return

start_num56#

資料切片依據,終止位置

57@property

58def

end_num(self):

59 end_num = self.current_page_num *self.per_page_counts

60return

end_num61#

拼接html標籤

62def

page_html(self):

63 tab_html = ''

64 tab_html += '

'106

return tab_html

函式低階版:

1

#函式低階版

2def pagenation(base_url,current_page_num,total_counts,per_page_counts=10,page_number=5):

3'''

4total_counts資料總數

5per_page_counts每頁分多少條資料

6page_number = 頁碼顯示多少個

7current_page_num 當前頁

8:return:

9'''10#

all_objs_list = models.customer.objects.all()11#

total_counts = all_objs_list.count()12#

page_number = 5

13try

:14 current_page_num =int(current_page_num)

15except

exception:

16 current_page_num = 1

17 half_page_range = page_number//218#

計算總頁數

19 page_number_count,a =divmod(total_counts,per_page_counts)

20if current_page_num < 1:

21 current_page_num = 1

22if

a:23 page_number_count += 1

24if current_page_num >page_number_count:

25 current_page_num =page_number_count

26 start_num = (current_page_num - 1) * 10

27 end_num = current_page_num * 10

28if page_number_count <=page_number:

29 page_start = 1

30 page_end =page_number_count

31else:32

if current_page_num <=half_page_range:

33 page_start = 1

34 page_end =page_number

35elif current_page_num + half_page_range >=page_number_count:

36 page_start = page_number_count - page_number + 1

37 page_end =page_number_count

38else

:39 page_start = current_page_num -half_page_range

40 page_end = current_page_num +half_page_range41#

拼接html標籤

42 tab_html = ''

43 tab_html += ''64

return tab_html,start_num,end_num

自定義分頁類

1.首先定義乙個分頁類 class page private function getpage private function first 顯示當前頁 list.this cpage.顯示當前頁之後的頁數 uri.page this pages.末頁 else private function s...

自定義類的封裝

開發工具與關鍵技術 vs mvc 當我們進行多表查詢,新增,修改,刪除的時候,要用到多個資料的表,這時候處理起來就比較麻煩,所以我們通過自定義類的方式來接收資料 例如這一句查詢語句,裡麵包過了,班級,年級,學生,還有使用者,我們繼續使用資料庫,建立的類來接收資料比較麻煩,所以我們就自定義乙個類來接收...

GridView自定義分頁類

今天查詢相關資料時,看到這個類寫的很好,就轉過來了啊,感謝原創者!下面直接是類的 一看就明白!public class pageing private int intpagecountww 0 總頁數 總頁數 public int getpagecount private string strpag...