Skip to content

Commit 9271280

Browse files
committed
fix paging
1 parent 96cbabb commit 9271280

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

modules/paginator/paginator.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ Output:
3636
type Paginator struct {
3737
total int // total rows count, -1 means unknown
3838
totalPages int // total pages count, -1 means unknown
39-
pagingNum int // how many rows in one page
4039
current int // current page number
41-
numPages int // how many pages to show on the UI
40+
curRows int // current page rows count
41+
42+
pagingNum int // how many rows in one page
43+
numPages int // how many pages to show on the UI
4244
}
4345

4446
// New initialize a new pagination calculation and returns a Paginator as result.
@@ -49,7 +51,26 @@ func New(total, pagingNum, current, numPages int) *Paginator {
4951
current = min(current, totalPages)
5052
}
5153
current = max(current, 1)
52-
return &Paginator{total, totalPages, pagingNum, current, numPages}
54+
return &Paginator{
55+
total: total,
56+
totalPages: totalPages,
57+
current: current,
58+
pagingNum: pagingNum,
59+
numPages: numPages,
60+
}
61+
}
62+
63+
func (p *Paginator) SetCurRows(rows int) {
64+
// For "unlimited paging", we need to know the rows of current page to determine if there is a next page.
65+
// There is still an edge case: when curRows==pagingNum, then the "next page" will be an empty page.
66+
// Ideally we should query one more row to determine if there is really a next page, but it's impossible in current framework.
67+
p.curRows = rows
68+
if p.total == -1 && p.current == 1 && !p.HasNext() {
69+
// if there is only one page for the "unlimited paging", set total rows/pages count
70+
// then the tmpl could decide to hide the nav bar.
71+
p.total = rows
72+
p.totalPages = 1
73+
}
5374
}
5475

5576
// IsFirst returns true if current page is the first page.
@@ -71,7 +92,10 @@ func (p *Paginator) Previous() int {
7192

7293
// HasNext returns true if there is a next page relative to current page.
7394
func (p *Paginator) HasNext() bool {
74-
return p.total == -1 || p.current*p.pagingNum < p.total
95+
if p.total == -1 {
96+
return p.curRows >= p.pagingNum
97+
}
98+
return p.current*p.pagingNum < p.total
7599
}
76100

77101
func (p *Paginator) Next() int {

routers/web/user/home.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,10 @@ func Dashboard(ctx *context.Context) {
137137
return
138138
}
139139

140-
ctx.Data["Feeds"] = feeds
141-
142-
pager := context.NewPagination(count, setting.UI.FeedPagingNum, page, 5)
140+
pager := context.NewPagination(count, setting.UI.FeedPagingNum, page, 5).WithCurRows(len(feeds))
143141
pager.AddParamFromRequest(ctx.Req)
144142
ctx.Data["Page"] = pager
143+
ctx.Data["Feeds"] = feeds
145144

146145
ctx.HTML(http.StatusOK, tplDashboard)
147146
}

services/context/pagination.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func NewPagination(total, pagingNum, current, numPages int) *Pagination {
2828
return p
2929
}
3030

31+
func (p *Pagination) WithCurRows(n int) *Pagination {
32+
p.Paginater.SetCurRows(n)
33+
return p
34+
}
35+
3136
func (p *Pagination) AddParamFromRequest(req *http.Request) {
3237
for key, values := range req.URL.Query() {
3338
if key == "page" || len(values) == 0 || (len(values) == 1 && values[0] == "") {

templates/user/dashboard/dashboard.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="flex-container-main">
66
{{template "base/alert" .}}
77
{{template "user/heatmap" .}}
8-
{{if .Feeds}}
8+
{{if .Page.Paginater.TotalPages}}
99
{{template "user/dashboard/feeds" .}}
1010
{{else}}
1111
{{template "user/dashboard/guide" .}}

0 commit comments

Comments
 (0)