自定義分頁和基於cookie實現定製顯示資料條數

2021-10-10 22:09:24 字數 4432 閱讀 8833

from django.utils.safestring import mark_safe

class

page

:def

__init__

(self,current_page,data_length,data_count=

10,page_count=10)

:"""

:param current_page: 當前顯示的頁數

:param data_length: 需要顯示的資料總長度

:param data_count: 每頁顯示的資料個數

:param page_count: 每頁顯示多少頁數鏈結

"""self.current_page = current_page

self.data_length = data_length

self.data_count = data_count

self.page_count = page_count

@property

defstart

(self)

:return

(self.current_page -1)

* self.data_count

@property

defend

(self)

:return self.current_page * self.data_count

@property

defpage_total

(self)

: page_of_total, v =

divmod

(self.data_length, self.data_count)

if v:

k = page_of_total +

1return page_of_total

@property

defstart_end_page

(self)

:if self.page_total < self.page_count +1:

start_page =

1 end_page = self.page_total +

1else

:if self.current_page <

(self.page_count +1)

/2: start_page =

1 end_page = self.page_count +

1else

: start_page = self.current_page -

(self.page_count -1)

/2end_page = self.current_page +

(self.page_count +1)

/2if end_page > self.page_total +1:

start_page = self.page_total - self.page_count +

1 end_page = self.page_total +

1return

int(start_page)

,int

(end_page)

defpage_str

(self)

: display_str =

prev_page = self.current_page -

1if prev_page >0:

prev_str =

.format

(prev_page)

else

: prev_str =

.format

(prev_page)

start_page, end_page = self.start_end_page

for i in

range

(start_page, end_page)

:if i == self.current_page:

str=

.format

(i, i)

else

:str=.

format

(i, i)

str)

after_page = self.current_page +

1if after_page >50:

after_str =

.format

(after_page)

else

: after_str =

.format

(after_page)

input_str =

'' go_str =

'go'

.format

(self.page_total)

# 將列表轉換為字串

display_str = mark_safe(

''.join(display_str)

)return display_str

from django.contrib import admin

from django.urls import path

from django.conf.urls import url,include

urlpatterns =

[ url(

'paging/$'

,views.paging)

,]

from utils import pagination

defpaging

(request)

:"""

current_page:當前顯示的頁數

start:當前頁顯示的起始資料

end:當前頁顯示的截止資料

data_count:每頁顯示的資料個數

page_total:總頁數

page_count:每頁顯示多少頁數鏈結

display_str:顯示分頁字串

start_page:每頁顯示的起始頁鏈結

end_page:每頁顯示的結束頁鏈結

:param request:

:return:

"""nid = request.get.get(

'p',1)

current_page =

int(nid)

data_count = request.cookies.get(

'pagination'

,none

)print

(data_count)

data_count =

int(data_count)

page = pagination.page(current_page,

len(user_list)

,data_count)

data = user_list[page.start:page.end]

display_str=page.page_str(

)return render(request,

'paging.html'

,)

href

="/static/paging.css"

rel="stylesheet"

/>

class

="warp"

>

>

ul>

div>

>

"sel"

class

="paging_icon"

>

value

="10"

>

10option

>

value

="30"

>

30option

>

value

="50"

>

50option

>

value

="100"

>

100option

>

select

>

div>

}div

>

#} >

}div

>

src=

"/static/jquery.cookie.js"

>

script

>

>$(

function()

)$('#go').

on('click'

,function()

else;}

);})

script

>

自定義分頁

分頁 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...

自定義分頁

分頁元件使用示例 1 先取出所有資料user list 2 例項化 obj pagination request.get.get page 1 len user list request 3 對所有資料列表切片 即切出一頁資料 page user list user list obj.start o...