Skip to content

Commit e2f13c0

Browse files
committed
fix
1 parent ea0223e commit e2f13c0

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

modules/paginator/paginator.go

Lines changed: 19 additions & 25 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
type Paginator struct {
35-
total int // total rows count
36-
pagingNum int // how many rows in one page
37-
current int // current page number
38-
numPages int // how many pages to show on the UI
35+
total int // total rows count
36+
totalPages int
37+
pagingNum int // how many rows in one page
38+
current int // current page number
39+
numPages int // how many pages to show on the UI
3940
}
4041

4142
// New initialize a new pagination calculation and returns a Paginator as result.
4243
func New(total, pagingNum, current, numPages int) *Paginator {
43-
if pagingNum <= 0 {
44-
pagingNum = 1
45-
}
46-
if current <= 0 {
47-
current = 1
48-
}
49-
p := &Paginator{total, pagingNum, current, numPages}
50-
if p.current > p.TotalPages() {
51-
p.current = p.TotalPages()
52-
}
44+
pagingNum = max(pagingNum, 1)
45+
totalPages := (total + pagingNum - 1) / pagingNum
46+
if total >= 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
return p
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-
return p.total > p.current*p.pagingNum
75+
return p.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-
if p.total == 0 {
88-
return true
89-
}
90-
return p.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-
if p.total == 0 {
101-
return 1
102-
}
103-
return (p.total + p.pagingNum - 1) / p.pagingNum
97+
return p.totalPages
10498
}
10599

106100
// Current returns current page number.
@@ -134,8 +128,8 @@ func getMiddleIdx(numPages int) int {
134128
// Pages returns a list of nearby page numbers relative to current page.
135129
// If value is -1 means "..." that more pages are not showing.
136130
func (p *Paginator) Pages() []*Page {
137-
if p.numPages == 0 {
138-
return []*Page{}
131+
if p.total == -1 || p.numPages == 0 {
132+
return nil
139133
} else if p.numPages == 1 && p.TotalPages() == 1 {
140134
// Only show current page.
141135
return []*Page{{1, true}}

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)