Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit19d11c3

Browse files
committed
fix
1 parentea0223e commit19d11c3

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

‎modules/paginator/paginator.go‎

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ Output:
3232

3333
// Paginator represents a set of results of pagination calculations.
3434
typePaginatorstruct {
35-
totalint// total rows count
36-
pagingNumint// how many rows in one page
37-
currentint// current page number
38-
numPagesint// how many pages to show on the UI
35+
totalint// total rows count
36+
totalPagesint
37+
pagingNumint// how many rows in one page
38+
currentint// current page number
39+
numPagesint// how many pages to show on the UI
3940
}
4041

4142
// New initialize a new pagination calculation and returns a Paginator as result.
4243
funcNew(total,pagingNum,current,numPagesint)*Paginator {
43-
ifpagingNum<=0 {
44-
pagingNum=1
45-
}
46-
ifcurrent<=0 {
47-
current=1
48-
}
49-
p:=&Paginator{total,pagingNum,current,numPages}
50-
ifp.current>p.TotalPages() {
51-
p.current=p.TotalPages()
52-
}
44+
pagingNum=max(pagingNum,1)
45+
totalPages:= (total+pagingNum-1)/pagingNum
46+
iftotal>=0 {
47+
current=min(current,totalPages)
48+
}else {
49+
totalPages=-1
50+
}
51+
current=max(current,1)
52+
p:=&Paginator{total,totalPages,pagingNum,current,numPages}
5353
returnp
5454
}
5555

@@ -72,7 +72,7 @@ func (p *Paginator) Previous() int {
7272

7373
// HasNext returns true if there is a next page relative to current page.
7474
func (p*Paginator)HasNext()bool {
75-
returnp.total>p.current*p.pagingNum
75+
returnp.total==-1||p.current*p.pagingNum<p.total
7676
}
7777

7878
func (p*Paginator)Next()int {
@@ -84,10 +84,7 @@ func (p *Paginator) Next() int {
8484

8585
// IsLast returns true if current page is the last page.
8686
func (p*Paginator)IsLast()bool {
87-
ifp.total==0 {
88-
returntrue
89-
}
90-
returnp.total> (p.current-1)*p.pagingNum&&!p.HasNext()
87+
return!p.HasNext()
9188
}
9289

9390
// Total returns number of total rows.
@@ -97,10 +94,7 @@ func (p *Paginator) Total() int {
9794

9895
// TotalPages returns number of total pages.
9996
func (p*Paginator)TotalPages()int {
100-
ifp.total==0 {
101-
return1
102-
}
103-
return (p.total+p.pagingNum-1)/p.pagingNum
97+
returnp.totalPages
10498
}
10599

106100
// Current returns current page number.
@@ -135,10 +129,10 @@ func getMiddleIdx(numPages int) int {
135129
// If value is -1 means "..." that more pages are not showing.
136130
func (p*Paginator)Pages() []*Page {
137131
ifp.numPages==0 {
138-
return[]*Page{}
139-
}elseifp.numPages==1&&p.TotalPages()==1 {
132+
returnnil
133+
}elseifp.total==-1|| (p.numPages==1&&p.TotalPages()==1) {
140134
// Only show current page.
141-
return []*Page{{1,true}}
135+
return []*Page{{p.current,true}}
142136
}
143137

144138
// Total page number is less or equal.

‎modules/paginator/paginator_test.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ func TestPaginator(t *testing.T) {
7676
t.Run("Only current page",func(t*testing.T) {
7777
p:=New(0,10,1,1)
7878
pages:=p.Pages()
79-
assert.Len(t,pages,1)
80-
assert.Equal(t,1,pages[0].Num())
81-
assert.True(t,pages[0].IsCurrent())
79+
assert.Empty(t,pages)// no "total", so no pages
8280

8381
p=New(1,10,1,1)
8482
pages=p.Pages()

‎templates/base/paginate.tmpl‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
{{$paginationLink := $.Link}}
33
{{if eq $paginationLink AppSubUrl}}{{$paginationLink = print $paginationLink "/"}}{{end}}
44
{{with .Page.Paginater}}
5-
{{if gt .TotalPages 1}}
5+
{{if .TotalPages}}
6+
{{$showFirstLast := gt .TotalPages 0}}
67
<div class="center page buttons">
78
<div class="ui borderless pagination menu">
9+
{{if $showFirstLast}}
810
<a class="{{if .IsFirst}}disabled{{end}} item navigation" {{if not .IsFirst}}href="{{$paginationLink}}{{if $paginationParams}}?{{$paginationParams}}{{end}}"{{end}}>
911
{{svg "gitea-double-chevron-left" 16 "tw-mr-1"}}
1012
<span class="navigation_label">{{ctx.Locale.Tr "admin.first_page"}}</span>
1113
</a>
14+
{{end}}
15+
1216
<a class="{{if not .HasPrevious}}disabled{{end}} item navigation" {{if .HasPrevious}}href="{{$paginationLink}}?page={{.Previous}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
1317
{{svg "octicon-chevron-left" 16 "tw-mr-1"}}
1418
<span class="navigation_label">{{ctx.Locale.Tr "repo.issues.previous"}}</span>
@@ -24,10 +28,13 @@
2428
<span class="navigation_label">{{ctx.Locale.Tr "repo.issues.next"}}</span>
2529
{{svg "octicon-chevron-right" 16 "tw-ml-1"}}
2630
</a>
31+
32+
{{if $showFirstLast}}
2733
<a class="{{if .IsLast}}disabled{{end}} item navigation" {{if not .IsLast}}href="{{$paginationLink}}?page={{.TotalPages}}{{if $paginationParams}}&{{$paginationParams}}{{end}}"{{end}}>
2834
<span class="navigation_label">{{ctx.Locale.Tr "admin.last_page"}}</span>
2935
{{svg "gitea-double-chevron-right" 16 "tw-ml-1"}}
3036
</a>
37+
{{end}}
3138
</div>
3239
</div>
3340
{{end}}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp