自定義分頁

2022-06-16 08:54:10 字數 3100 閱讀 5778

"""

分頁元件使用示例:

1) 先取出所有資料user_list

2) 例項化:

obj = pagination(request.get.get('page',1), len(user_list), request)

3) 對所有資料列表切片(即切出一頁資料):

page_user_list = user_list[obj.start:obj.end]

4) 返回給頁面:

return render(request,'index.html',)

5) 模板頁面通過 } 渲染頁碼

"""

class

pagination(object):

#第一步 初始化頁面

def__init__(self,current_page_num,all_count,request,per_page_num=5,pager_count=11):

"""封裝分頁相關資料

:param current_page_num: 當前訪問頁的數字

:param all_count: 分頁資料中的資料總條數

:param per_page_num: 每頁顯示的資料條數

:param pager_count: 最多顯示的頁碼個數

"""try

: current_page_num =int(current_page_num)

except

exception as e:

current_page_num = 1

if current_page_num <1:

current_page_num = 1self.current_page_num =current_page_num

self.all_count =all_count

self.per_page_num =per_page_num

#第二步 實際總頁碼

all_pager, tmp =divmod(all_count, per_page_num)

iftmp:

all_pager += 1self.all_pager =all_pager

self.pager_count =pager_count

self.pager_count_half = int((pager_count - 1) / 2) #5#

儲存搜尋條件

import

copy

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

@property

#屬性方法 在外部views裡 切片的時候不用加括號 直接呼叫 start就可以

defstart(self):

return (self.current_page_num - 1) *self.per_page_num

@property

defend(self):

return self.current_page_num *self.per_page_num

defpage_html(self):

#如果總頁碼 < 11個:

if self.all_pager <=self.pager_count:

pager_start = 1pager_end = self.all_pager + 1

#總頁碼 > 11

else

:

#當前頁如果<=頁面上最多顯示11/2個頁碼

if self.current_page_num <=self.pager_count_half:

pager_start = 1pager_end = self.pager_count + 1

#當前頁大於5

else

:

#頁碼翻到最後

if (self.current_page_num + self.pager_count_half) >self.all_pager:

pager_start = self.all_pager - self.pager_count + 1pager_end = self.all_pager + 1

else

: pager_start = self.current_page_num -self.pager_count_half

pager_end = self.current_page_num + self.pager_count_half + 1page_html_list =

first_page = '

首頁' % (1,)

if self.current_page_num <= 1:

prev_page = '

'else

: prev_page = '

' % (self.current_page_num - 1,)

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

#主要的for迴圈

for i in

range(pager_start, pager_end):

self.params[

"page

"]=i

if i ==self.current_page_num:

temp = '

%s' %(self.params.urlencode(),i)

else

: temp = '

%s' %(self.params.urlencode(),i,)

if self.current_page_num >=self.all_pager:

next_page = '

'else

: next_page = '

' % (self.current_page_num + 1,)

last_page = '

尾頁' %(self.all_pager,)

return

''.join(page_html_list)

自定義分頁

分頁 public string madebuttonlink int pagecount,int curpage,string szurl pagecount得到頁數 curpage當前頁 szurl連線位址 public int getpagecount string sztable,int l...

自定義分頁

前台 後台 資料繫結 private void gridview bind sql order by c.aid,c.itemid,c.score datatable dt dataaccess.getdatatable sql pageddatasource ps new pageddatasou...

antd自定義分頁器 自定義分頁器例項

def init self,current page num,all count,request,per page num 2,pager count 11 封裝分頁相關資料 param current page num 當前訪問頁的數字 param all count 分頁資料中的資料總條數 pa...